1

I looked at a very similar question but was unable to resolve the issue Replace comma with newline in sed

I am trying to convert : characters in a string. This is what I tried:

echo -e 'this:is:a:test' | sed "s/\:/'\n'/g"

but this replaces : with n. I tried tr too but had the same result. I believe the -e is not seen after being piped so new line is not recognized.

Any help is appreciated.

Community
  • 1
  • 1
yatici
  • 537
  • 3
  • 10
  • 19
  • what system are you on? Your command pipeline works for me on RHEL – iruvar May 15 '13 at 12:52
  • GNU `sed` appears to process `\n` as a newline (also in the much simpler `sed 's/:/\n/g'`; BSD `sed` does not. – chepner May 15 '13 at 12:55
  • yea that still has the same output for me : changes to n. This works tho which is weird echo -e ${PATH//:/'\n'} which I don't really get the logic why. – yatici May 15 '13 at 13:01
  • 3
    You don't need the -e in the echo, that's for expanding meta-characters in the output of the echo statement, not the output of sed. You should be able to include a newline in sed without any escaping or quoting: **echo 'this:is:a:test' | sed "s/:/\n/g"** should work. If it doesn't, do a **sed --version** and **echo $SHELL** to let us know what your environment is like. – jesse_galley May 15 '13 at 13:14
  • Hi echo $SHELL gives me /bin/tcsh. But sed --version or sed -v doesn't work says unknown flag. I ran pkginfo -l SUNWcsu and it says 11.10.0 – yatici May 15 '13 at 13:34
  • I am curious why you need to escape `:` in sed... – anishsane May 15 '13 at 13:41

4 Answers4

5
echo 'this:is:a:test' | tr : \\n

Any POSIX-compliant tr will support the \n escape sequence. You need to take care to quote or escape the escape sequence, however (double backslash above).

The -e argument to echo has no effect on your argument to echo.

pilcrow
  • 51,771
  • 11
  • 83
  • 128
  • +1 because this is the most lightweight solution. May not portable across ALL POSSIBLE shells. (one of my stupid busybox based shell doesn't have tr command itself...) – anishsane May 15 '13 at 13:47
  • I wished it worked but no luck. Perl one works and creating variables and calling it works. This is probably an issue with my system. sigh.. Is there a way to update my commands such as? tr, grep, sed, etc... – yatici May 15 '13 at 20:21
4

I'll presume that you have the string in a variable already. This uses the parameter expansion substitution operator to replace every : with a newline, which is specified using a $'...'-quoted string. Both features are bash extensions to the standard, and may not work in another shell.

$ foo="this:is:a:test"
$ bar="${foo//:/$'\n'}"
$ echo "$bar"
this
is
a
test
chepner
  • 389,128
  • 51
  • 403
  • 529
1

You do not need echo -e because you have \n in sed, not in echo statement. So, the following should work (note that I have changed '\n' to \n):

echo -e 'this:is:a:test' | sed "s/\:/\n/g"

or

echo  'this:is:a:test' | sed "s/\:/\n/g"

Also note that you do not need to escape : so the following will work too (thanks to @anishsane)

echo  'this:is:a:test' | sed "s/:/\n/g"

Below is just to reiterate why you need -e for echo

$ echo -e "hello \n"
hello

$ echo  "hello \n"
hello \n
Bill
  • 4,477
  • 5
  • 28
  • 49
  • you dont, it works without it...i was just using the code posted...will update the answer..thanks – Bill May 15 '13 at 13:44
1

Perhaps Perl is an option?

echo -e 'this:is:a:test' | perl -p -e 's/:/\n/g'
Wolph
  • 69,888
  • 9
  • 125
  • 143
  • I'm not a perl fan but if you want consistent cross-platform search/replace actions than it's not such a bad method imho :) Especially when also using the `-i` (in-place) flag. – Wolph May 15 '13 at 13:41