Converting column to rows for every 3 lines in the column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Converting column to rows for every 3 lines in the column
# 1  
Old 05-21-2012
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! Smilie
# 2  
Old 05-21-2012
Code:
$ awk '{printf("%s%s",$0,NR%3?",":"\n")}' input
a,b,c
d,e,f
g,h,i
j,

This User Gave Thanks to neutronscott For This Post:
# 3  
Old 05-21-2012
Code:
$ paste -d, - - - < infile
a,b,c
d,e,f
g,h,i
j,,

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 4  
Old 05-21-2012
Code:
awk '{ORS=NR%3?",":"\n"}' filename

This User Gave Thanks to pandeesh For This Post:
# 5  
Old 05-23-2012
thanks guys! all of these worked for me.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting Single Column into Multiple rows

Hi .. anyone can you help me ? i need to convert text below into multiple columns interface; GigabitEthernet0/0/0/0 description; TRUNK_PE-D2-JT2-VPN_Gi0/0/0/0_TO_ME4-A-JKT-JT_4/1/1_1G mtu 9212 negotiation auto interface; GigabitEthernet0/0/0/0.11 description; tes encapsulation;... (1 Reply)
Discussion started by: mad3linux
1 Replies

2. Shell Programming and Scripting

Converting Single Column into Multiple rows, but with strings to specific tab column

Dear fellows, I need your help. I'm trying to write a script to convert a single column into multiple rows. But it need to recognize the beginning of the string and set it to its specific Column number. Each Line (loop) begins with digit (RANGE). At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies

3. UNIX for Dummies Questions & Answers

awk - Extract 4 lines in Column to Rows Tab Delimited between tags

I have tried the following to no avail. xargs -n8 < test.txt awk '{if(NR%6!=0){p=""}else{p="\n"};printf $0" "p}' Mod_Alm_log.txt > test.txt I have tried different variations of the above, the problem is mixes lines together. And it includes the tags "%a and %A" I need them to be all tab... (16 Replies)
Discussion started by: mytouchsr
16 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Deleting all rows where the first column equals the second column

Hi, I have a tab delimited text file where the first two columns equal numbers. I want to delete all rows where the value in the first column equals the second column. How do I go about doing that? Thanks! Input: 1 1 ABC DEF 2 2 IJK LMN 1 2 ZYX OPW Output: 1 2 ZYX OPW (2 Replies)
Discussion started by: evelibertine
2 Replies

5. Shell Programming and Scripting

awk command to print only selected rows in a particular column specified by column name

Dear All, I have a data file input.csv like below. (Only five column shown here for example.) Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 3,2,4,5,6 5,3,5,5,6 From this I want the below output Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 where the second column... (4 Replies)
Discussion started by: ks_reddy
4 Replies

6. Shell Programming and Scripting

Converting rows to column

i have output of script as below name,roll_no,01-05-12,02-05-12,03-05-12 sam,12,24,24,24 jon,145,24,24,22 van,29,24,22,24 i want to convert these into columns as output is not fixed please tell me how to convert 1st row in to 1st columns likewise,as many rows are there are to be converted... (4 Replies)
Discussion started by: sagar_1986
4 Replies

7. Shell Programming and Scripting

Converting entries from rows to column

Hi all, I need your help on a multiple row entry into different columns. And do the same with all the entries in file. File example (showing 2 entries only, there are many like these): >ABC * AGA-AUUCUC-CGGUUCAAUCU ||| UCUAUAACCGCGCCGAGUUAGU >ABC * AGAUAU-GCUGCAGGCUCAAUUG ||||||... (2 Replies)
Discussion started by: atulkakrana
2 Replies

8. Shell Programming and Scripting

command for converting 4 column data to 1 column

dear friends I want to convert four column data to one column data. For example: from 1, 2, 3, 4 5, 6, 7, 8to 1 2 3 4 5 6 7 8what is the general command for that type of convertion. thanks (5 Replies)
Discussion started by: rpf
5 Replies

9. Shell Programming and Scripting

Converting Single Column into Multiple rows

i have single column which is starting with same string(many number of rows) i have to convert each into a single row.how can i do that? laknar std mes 23 55 laknar isd phone no address amount 99 I have to convert above like below. laknar|std|mes|23|55 laknar|isd|phone... (3 Replies)
Discussion started by: laknar
3 Replies

10. Shell Programming and Scripting

Converting Column to Rows in a Flat file

Hi, Request To guide me in writing a shell program for the following requirement: Example:if the Input File contains the follwing data Input File Data: 80723240029,12,323,443,88,98,7,98,67,87 80723240030,12,56,6,,,3,12,56,6,7,2,3,12,56,6,7,2,3,88,98,7,98,67,87... (5 Replies)
Discussion started by: srinikal
5 Replies
Login or Register to Ask a Question