0

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
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

3 Answers3

0

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

  • Thanks for your reply however it doesnt give me what I need. it changes ; into new rows but I need the value "30" in each line too. Any ideas? – joshiricky Feb 20 '17 at 16:40
0

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
steeldriver
  • 81,074
0

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.