Find the answer to your Linux question:
Results 1 to 5 of 5
I'm a doing a script. There are some part of the script that I need to do but don't know how to do it. If I have a text which ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Mar 2006
    Posts
    101

    it this possible in bash script


    I'm a doing a script. There are some part of the script that I need to do but don't know how to do it.

    If I have a text which contains:

    foo,fee,fii,fuu
    Could I rearrange it to:

    foo
    fee
    fii
    fuu
    I don't know if sed can do this. There are some function of sed that I just recently discovered but I don't know if it can do the scenario above.

    I'm currently checking a tool/command that could help me do this thing just wandering if there's anyone out there that could suggest on how I could accomplish this.

  2. #2
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    sed can do this, right.

    But you can also use tr in order to replace ',' by newline ('\n').
    Debian GNU/Linux -- You know you want it.

  3. #3
    Linux Newbie
    Join Date
    Mar 2006
    Posts
    101
    Code:
    sed -i 's/,/\n/g' YOUR-FILE.txt
    This one do the trick

  4. #4
    Just Joined! jr0sco's Avatar
    Join Date
    Aug 2010
    Location
    Australia
    Posts
    41
    If string is in a variable try this.

    Code:
    foo=foo,fee,fii,fuu 
    
    echo $foo | sed 's/,/\n/g'

  5. #5
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by packets View Post
    I'm a doing a script. There are some part of the script that I need to do but don't know how to do it.

    If I have a text which contains:



    Could I rearrange it to:



    I don't know if sed can do this. There are some function of sed that I just recently discovered but I don't know if it can do the scenario above.

    I'm currently checking a tool/command that could help me do this thing just wandering if there's anyone out there that could suggest on how I could accomplish this.
    if you are using some kind of modern shell, eg bash, ksh etc, they already have internal capabilities to string manipulation
    Code:
    $  text="foo,fee,fii,fuu"
    $ IFS=","
    $ set -- $text
    $ echo $1
    foo
    $ echo $2
    fee
    $ for((i=1;i<=${#};i++))
    > do
    >  eval echo \$${i}
    > done
    foo
    fee
    fii
    fuu

Posting Permissions

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