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?
This question is related to
linux
bash
sed
string-split
This should do it:
cat ~/Desktop/myfile.txt | sed s/:/\\n/g
This might work for you (GNU sed):
sed 'y/:/\n/' file
or perhaps:
sed y/:/$"\n"/ file
Using \n
in sed
is non-portable. The portable way to do what you want with sed
is:
sed 's/:/\
/g' ~/Desktop/myfile.txt
but in reality this isn't a job for sed
anyway, it's the job tr
was created to do:
tr ':' '
' < ~/Desktop/myfile.txt
If you're using gnu sed then you can use \x0A
for newline:
sed 's/:/\x0A/g' ~/Desktop/myfile.txt
Source: Stackoverflow.com