Find the answer to your Linux question:
Results 1 to 7 of 7
I'm creating a BASH script where mysql is involve. I can query data to the shell using "mysql -e" commands. I have to query my mysql to get data. The ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Mar 2006
    Posts
    101

    input new line for every match input


    I'm creating a BASH script where mysql is involve. I can query data to the shell using "mysql -e" commands. I have to query my mysql to get data. The data is in the ff format

    data1,data2,data3,data4

    I need to make commas to be a new line and remove commas.Is there a way I can output it to the ff format:

    data1
    data2
    data3
    data4

    I don't want to use perl since I'm not expert on it. I just want it to be simple.

  2. #2
    Just Joined!
    Join Date
    Dec 2006
    Posts
    85
    well im not much of an expert on bash, so here is the messy way:
    echo "data1,data2,data3,data4"|sed -e 's/data1,/data1\n/' -e 's/data2,/data2\n/' -e 's/data3,/data3\n/' -e 's/data4,/data4\n/'

    that will echo it into that format...im sure someone else here will be able to gie you a prettier way...

  3. #3
    Linux Newbie egan's Avatar
    Join Date
    Feb 2009
    Location
    Mountain View, CA
    Posts
    132
    Or you can translate commas into newlines?
    cat $INFILE | tr "," "\n" | $OUTFILE

  4. #4
    Just Joined!
    Join Date
    Dec 2006
    Posts
    85
    Quote Originally Posted by egan View Post
    Or you can translate commas into newlines?
    cat $INFILE | tr "," "\n" | $OUTFILE
    see...this is what i mean. i had never even heard of the translate function until this very moment.

  5. #5
    Linux Newbie egan's Avatar
    Join Date
    Feb 2009
    Location
    Mountain View, CA
    Posts
    132
    Quote Originally Posted by the_ultimate_samurai View Post
    see...this is what i mean. i had never even heard of the translate function until this very moment.
    You'd be surprised how many different tools there are to get basic jobs such as these done. That's part of the beauty of Unix and its history.
    Here is a basic run down of common Unix text processing tools:
    Text Processing Commands
    One of my favorites is 'cut'.

  6. #6
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by egan View Post
    Or you can translate commas into newlines?
    cat $INFILE | tr "," "\n" | $OUTFILE
    no need cat. its useless
    Code:
    tr .... < file

  7. #7
    Linux Newbie egan's Avatar
    Join Date
    Feb 2009
    Location
    Mountain View, CA
    Posts
    132
    Quote Originally Posted by ghostdog74 View Post
    no need cat. its useless
    Code:
    tr .... < file
    Oh yes, I just copied and pasted that out of a much more complicated script...

    tr "," "\n" < $INFILE > $OUTFILE

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •