LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-26-2010, 06:18 AM   #1
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 168

Rep: Reputation: 15
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
 
Old 11-26-2010, 06:27 AM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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.
 
1 members found this post helpful.
Old 11-26-2010, 06:42 AM   #3
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 168

Original Poster
Rep: Reputation: 15
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. :-)
 
Old 11-26-2010, 06:43 AM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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!

Last edited by GrapefruiTgirl; 11-26-2010 at 06:46 AM.
 
1 members found this post helpful.
Old 11-26-2010, 07:47 AM   #5
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 168

Original Poster
Rep: Reputation: 15
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 :-)

Last edited by hattori.hanzo; 11-26-2010 at 08:10 AM.
 
Old 11-26-2010, 08:51 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,627

Rep: Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943Reputation: 2943
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
 
1 members found this post helpful.
Old 11-26-2010, 08:53 AM   #7
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by grail View Post
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.
 
Old 11-26-2010, 10:16 AM   #8
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 346

Rep: Reputation: 152Reputation: 152
Using GNU sed
Code:
sed 'N;N;N;N;s/\n/,/g' infile

Last edited by Kenhelm; 11-26-2010 at 10:18 AM.
 
2 members found this post helpful.
Old 11-26-2010, 10:28 AM   #9
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
@ 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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Join lines in text file vidyashankara Linux - General 10 12-21-2009 04:17 PM
Unable to join domain using Net Join command in FC3 client jeb083079 Linux - Networking 9 07-30-2007 03:41 AM
join lines until a $ is found powah Programming 2 07-26-2007 04:19 AM
Join all lines using sed chipix Programming 3 04-03-2007 10:55 AM
join every three lines of a text file powah Programming 8 02-02-2007 12:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration