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 Code:
2,111.222.333.444,host.edu,IP Category,John Doe |
Perhaps this will be useful:
Code:
awk '{ printf $0;count++; if(count!=5){ printf "," } else { print ""; count=0 } }' inputfile Code:
root@reactor: awk '{printf $0;count++; if (count!=5){printf ","}else{print ""; count=0}}' inputfile |
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. :-) |
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! |
I'll didnt know about the RS variable but will look into it. Thx.
I was using Code:
sed 's/.$//' |
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 |
Quote:
|
Using GNU sed
Code:
sed 'N;N;N;N;s/\n/,/g' infile |
@ 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. |