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

3 Answers 3

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
1
  • Perfect!!! Thank you very much steeldriver:)
    – joshiricky
    Feb 20, 2017 at 16:54
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

1
  • 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, 2017 at 16:40
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.

3
  • Thanks for that Rakesh. I did try some versions of sed but couldn't find anything that worked.
    – joshiricky
    Feb 21, 2017 at 14:23
  • Did you get any errors regarding "event G: not found" ? Feb 21, 2017 at 14:32
  • No, the way I did it just replaced each ";" with a new line but didnt transfer over the "30".
    – joshiricky
    Feb 22, 2017 at 15:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .