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
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
  • Perfect!!! Thank you very much steeldriver:) – joshiricky Feb 20 '17 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

  • 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

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.

  • Thanks for that Rakesh. I did try some versions of sed but couldn't find anything that worked. – joshiricky Feb 21 '17 at 14:23
  • Did you get any errors regarding "event G: not found" ? – Rakesh Sharma Feb 21 '17 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 '17 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.