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.
You do not need
echo -e
because you have\n
insed
, not inecho
statement. So, the following should work (note that I have changed'\n'
to\n
):or
Also note that you do not need to escape
:
so the following will work too (thanks to @anishsane)Below is just to reiterate why you need
-e
forecho
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 arebash
extensions to the standard, and may not work in another shell.Perhaps Perl is an option?
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.