LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Join 5 lines blocks comma delimited (https://www.linuxquestions.org/questions/programming-9/join-5-lines-blocks-comma-delimited-846673/)

hattori.hanzo 11-26-2010 05:18 AM

Join 5 lines blocks comma delimited
 
I have been searching previous posts but could not find an example which works with my data. I think I might be the spaces in my fields.

I have a massive data file and need to join 5 line blocks separated by a comma.

From:
Code:

2
111.222.333.444
host.edu
IP Category
John Doe
4
555.666.777.888
host2.com
IP Category
Mary Jane

To:
Code:

2,111.222.333.444,host.edu,IP Category,John Doe
4,555.666.777.888,host2.come,IP Category,Mary Jane

Thanks & Regards

GrapefruiTgirl 11-26-2010 05:27 AM

Perhaps this will be useful:
Code:

awk '{ printf $0;count++; if(count!=5){ printf "," } else { print ""; count=0 } }' inputfile
Seems to work for me:
Code:

root@reactor: awk '{printf $0;count++; if (count!=5){printf ","}else{print ""; count=0}}' inputfile
2,111.222.333.444,host.edu,IP Category,John Doe
4,555.666.777.888,host2.com,IP Category,Mary Jane

root@reactor:

Cheers. :)

hattori.hanzo 11-26-2010 05:42 AM

Thanks GrapefruiTgirl.

Initially, it didnt work cause the file had Windows CRLF but after converting it to Unix LF the awk command works perfectly.

Have a great weekend. :-)

GrapefruiTgirl 11-26-2010 05:43 AM

Good work! Thanks, you have a nice weekend too.

PS - for future reference (maybe you already know this) remember awk's RS variable, which would probably have helped, if you wanted it to, in the case of Windows/DOS line endings. RS is the record separator.

Cheers!

hattori.hanzo 11-26-2010 06:47 AM

I'll didnt know about the RS variable but will look into it. Thx.

I was using
Code:

sed 's/.$//'
then piped the output to your awk statement. Not as elegant but it got the job done :-)

grail 11-26-2010 07:51 AM

Well along with RS is ORS ... so see how this one blows your mind ;)
Code:

awk 'x++ == 4{printf "%s\n",$0;x=0;next}1' ORS="," file

GrapefruiTgirl 11-26-2010 07:53 AM

Quote:

Originally Posted by grail (Post 4171751)
Well along with RS is ORS ... so see how this one blows your mind ;)
Code:

awk 'x++ == 4{printf "%s\n",$0;x=0;next}1' ORS="," file

Excellent! Very clever. ;)

Kenhelm 11-26-2010 09:16 AM

Using GNU sed
Code:

sed 'N;N;N;N;s/\n/,/g' infile

GrapefruiTgirl 11-26-2010 09:28 AM

@ Kenhelm,

I would probably not have considered sed for this, but that's pretty clever too - and involves the least typing so far.. :)

Thanks for sharing it!


All times are GMT -5. The time now is 10:30 PM.