I want to split a text into rows and print its value, for example, change this:
YLB; YLR; YLS (tab) 30
Into this:
YLB (tab) 30
YLR (tab) 30
YLS (tab) 30
I want to split a text into rows and print its value, for example, change this:
YLB; YLR; YLS (tab) 30
Into this:
YLB (tab) 30
YLR (tab) 30
YLS (tab) 30
Try this
sed 's/; /\n/g' yourfile
See this post: https://stackoverflow.com/questions/18486203/to-insert-line-breaks-in-a-file-whenever-a-comma-is-encountered-shell-script
Using awk
awk 'BEGIN {FS="[; \t]+"; OFS="\t"} {for (i=1; i<NF; i++) print $i, $NF}'
Ex.
$ echo 'YLB; YLR; YLS 30' | awk 'BEGIN {FS="[; \t]+"; OFS="\t"} {for (i=1; i<NF; i++) print $i, $NF}'
YLB 30
YLR 30
YLS 30
This is one way you could do it with "sed":
TAB=`echo 'x' | tr 'x' '\011'`; # tab
SPC=`echo 'x' | tr 'x' '\040'`; # space
WS="[${SPC}${TAB}]"; # whitespace regex
echo "YLB; YLR; YLS ${TAB} 30" |
sed -ne "
/\n/!G
s/^\([^;]*\);${WS}*\(.*\)\(${WS}${TAB}.*\)/\1\3\2\3/
P;/\n.*\n/D
"
We have to have the shell variables TAB/SPC/WS as the POSIX "sed" does not support \t \s hence this is a poor-man's implementation of those.