Tuesday, July 15, 2008
Break a line into multiple lines - awk, sed
Input line:
ASX,23,PILE2.3,MNP,54,NEW123,LELO,67,PNP2
Output required: 3 entries in one line i.e.
ASX,23,PILE2.3
MNP,54,NEW123
LELO,67,PNP2
awk and sed solution:
$ LINE="ASX,23,PILE2.3,MNP,54,NEW123,LELO,67,PNP2"
$ echo $LINE | awk -F, '{for(i=1;i<=NF;i++){printf("%s%s",$i,i%3?",":"\n")}}'
$ echo $LINE | sed 's/\([^,]*,[^,]*,[^,]*,\)/&\
/g'
$ echo $LINE | sed 's/\([^,]*,\)\{3\}/&\
/g'
Similar post: split a line into multiple lines using or sed
Subscribe to:
Post Comments (Atom)
© Jadu Saikia www.UNIXCL.com
5 comments:
You awk script does the right thing:
$ echo "ASX,23,PILE2.3,MNP,54,NEW123,LELO,67,PNP2" | awk -F, '{for(i=1;i<=NF;i++){printf("%s%s",$i,i%3?",":"\n")}}'
ASX,23,PILE2.3
MNP,54,NEW123
LELO,67,PNP2
But I don't think your sed script is quite right:
$ echo "ASX,23,PILE2.3,MNP,54,NEW123,LELO,67,PNP2" | sed 's/\([^,]*,[^,]*,[^,]*,\)/&\
/g'
ASX,23,PILE2.3,
MNP,54,NEW123,
LELO,67,PNP2
with semicolons at the end of the lines. To get around this, try using
$ echo "ASX,23,PILE2.3,MNP,54,NEW123,LELO,67,PNP2" | sed 's/\([^,]*,[^,]*,[^,]*\),/\1\
/g'
ASX,23,PILE2.3
MNP,54,NEW123
LELO,67,PNP2
hey, you are absolutely right. I missed that. Thanks for your post and your tip.
Thanks.
Hi Guys, i am very new to support analyst role. i have a big error file (75 mb) and which consists only one line!! yes only one big big line. i now have to find particular error details occured 200 times in line. since customer ID changes in every occurence only consistent line is "Payment already allocated". i now have to print 150 characters before and 100 characters after this statement. Again there is only one line in file
please help
@Ritesh,thanks for commenting.
This example might help you:
$ echo "abcdefghij0123456789" | awk -v numchr=3 'match($0,"f"){print substr($0,RSTART-numchr,numchr),substr($0,RSTART+1,numchr)}'
Output:
cde ghi
Feel free to put a comment if you require some more assistance.
sed 's/,/\n/3;P;D'
Post a Comment