I have a string in the following format:
string1:string2:string3:string4:string5
I'm trying to use sed
to split the string on :
and print each sub-string on a new line. Here is what I'm doing:
cat ~/Desktop/myfile.txt | sed s/:/\\n/
This prints:
string1
string2:string3:string4:string5
How can I get it to split on each delimiter?
Using simply tr :
If you really need sed :
This should do it:
If you're using gnu sed then you can use
\x0A
for newline:Using
\n
insed
is non-portable. The portable way to do what you want withsed
is:but in reality this isn't a job for
sed
anyway, it's the jobtr
was created to do:To split a string with a delimiter with GNU sed you say:
For example, to split using
:
as a delimiter:Or with a non-GNU sed:
In this particular case, you missed the
g
after the substitution. Hence, it is just done once. See:g
stands forg
lobal and means that the substitution has to be done globally, that is, for any occurrence. See that the default is 1 and if you put for example 2, it is done 2 times, etc.All together, in your case you would need to use:
Note that you can directly use the
sed ... file
syntax, instead of unnecessary piping:cat file | sed
.This might work for you (GNU sed):
or perhaps: