Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-21-2012
Registered User
 
Join Date: Jan 2012
Posts: 29
Thanks: 17
Thanked 0 Times in 0 Posts
Converting column to rows for every 3 lines in the column

Hi gurus!

Please help me with this one. I have an file with the following contents:

Code:
a
b
c
d
e
f
g
h
i
j

I would like to make to transform it to look like this as my output file:

Code:
a,b,c
d,e,f
g,h,i
j

So for every 3 lines of the input file is read, that three lines of the column will converted into a comma delimited row. This should be done for every 3 lines until it reaches the end of the input file.


Any thoughts of using awk?

Thanks in advance!
Sponsored Links
    #2  
Old 05-21-2012
neutronscott's Avatar
script kiddie
 
Join Date: Jun 2011
Location: Charleston, SC
Posts: 649
Thanks: 18
Thanked 189 Times in 179 Posts

Code:
$ awk '{printf("%s%s",$0,NR%3?",":"\n")}' input
a,b,c
d,e,f
g,h,i
j,

The Following User Says Thank You to neutronscott For This Useful Post:
kokoro (05-23-2012)
Sponsored Links
    #3  
Old 05-21-2012
Scrutinizer's Avatar
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,346
Thanks: 144
Thanked 1,755 Times in 1,592 Posts

Code:
$ paste -d, - - - < infile
a,b,c
d,e,f
g,h,i
j,,

The Following 2 Users Say Thank You to Scrutinizer For This Useful Post:
47shailesh (05-21-2012), kokoro (05-23-2012)
    #4  
Old 05-21-2012
pandeesh's Avatar
Registered User
 
Join Date: Jul 2011
Posts: 311
Thanks: 75
Thanked 12 Times in 12 Posts

Code:
awk '{ORS=NR%3?",":"\n"}' filename

The Following User Says Thank You to pandeesh For This Useful Post:
kokoro (05-23-2012)
Sponsored Links
    #5  
Old 05-23-2012
Registered User
 
Join Date: Jan 2012
Posts: 29
Thanks: 17
Thanked 0 Times in 0 Posts
thanks guys! all of these worked for me.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Converting rows to column sagar_1986 Shell Programming and Scripting 4 05-04-2012 02:07 AM
Converting entries from rows to column atulkakrana Shell Programming and Scripting 2 02-17-2012 02:59 AM
command for converting 4 column data to 1 column rpf Shell Programming and Scripting 5 12-12-2011 10:40 AM
Converting Single Column into Multiple rows laknar Shell Programming and Scripting 3 01-24-2009 09:40 AM
Converting Column to Rows in a Flat file srinikal Shell Programming and Scripting 5 10-10-2008 03:32 PM



All times are GMT -4. The time now is 09:41 AM.