Convert columns to rows in perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert columns to rows in perl script
# 1  
Old 05-19-2012
Convert columns to rows in perl script

Hi All

I want to have a Perl script which convert columns to rows.
The Perl should should read the data from input file.
Suppose the input file is
Code:
7215484
date to date
173.3
A
1.50
2.23
8.45
10.14
2.00
4.50
2.50
31.32
7216154
month to month
182.7
A
1.50
2.36
8.91
10.69
2.00
4.50
4.16
34.11
7216184
case to case
179.6
A
1.50
2.32
8.76
10.51
2.00
4.50
4.16
33.75
7216254
done with no error
193.0
A
1.50
2.49
9.41
11.29
2.00
4.50
4.16
35.35

and the output file wanted is
Code:
7215484	"date to date"	173.3	A	1.50	2.23	8.45	10.14	2.00	4.50	2.50	31.32
7216154	"month to month"	182.7	A	1.50	2.36	8.91	10.69	2.00	4.50	4.16	34.11
7216184	"case to case"	179.6	A	1.50	2.32	8.76	10.51	2.00	4.50	4.16	33.75
7216254	"done with no error"	193.0	A	1.50	2.49	9.41	11.29	2.00	4.50	4.16	35.35

# 2  
Old 05-19-2012
With awk, you can use:
Code:
awk '{if(a!=""){a=a "\t" $0}else{a=$0}} NR%12 == 0{print a; a=""}' infile

With perl, the code is a little bit more verbose.
Code:
perl -le 'while(<>){chomp; if($a!=""){$a="$a\t$_"}else{$a=$_} if($.%12==0){print $a; $a=""}}' infile

Note: the second column is not quoted, I don't think it needs to be, because I use the \t character as field separator.

Last edited by kevintse; 05-19-2012 at 04:07 AM..
# 3  
Old 05-19-2012
Alternatively:
Code:
awk 'ORS=!(NR%12)?RS:"\t"' infile

Code:
paste - - - - - - - - - - - - < infile

Code:
perl -lpe '$\=!($.%12)?"\n":"\t"' infile


Last edited by Scrutinizer; 05-19-2012 at 05:23 AM..
# 4  
Old 05-19-2012
Quote:
Originally Posted by Scrutinizer
Alternatively:
Code:
awk 'ORS=!(NR%12)?RS:"\t"' infile

Code:
paste - - - - - - - - - - - - < infile

Code:
perl -lpe '$\=!($.%12)?"\n":"\t"' infile

Cool, you just changed the output record separator accordingly.
I am just using the tools, while you are playing with the tools.
That was amazing!
This User Gave Thanks to kevintse For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with script to convert rows to columns

Hello I have a large database with the following structure: Headword=Gloss1;Gloss2;Gloss3 The Glosses are separated by a ; What I need is to reduce the multiple glosses on each row to columns Headword=Gloss1 Headword=Gloss2 Headword=Gloss3 I had written the following script in awk... (5 Replies)
Discussion started by: gimley
5 Replies

2. Shell Programming and Scripting

Shell script to convert rows to columns

Hi I have a file having the values like below ---------------------------- .set A col1=”ABC” col2=34 col3=”DEF” col4=”LMN” col5=25 .set A .set B col1=55 col3=”XYZ” col4=”PQR” col5=66 .set B .set C col2=”NNN” (1 Reply)
Discussion started by: Suneel Mekala
1 Replies

3. Shell Programming and Scripting

Convert rows into columns using awk or perl

hi friends, i am able to parse cvs diff file using bit of cut and grep commands to produce following output in text file '''cvs-diff.txt''' Package-Name = dev-freetype. Old-Version = 2.4.8 New-Version = 2.4.10 Patches-removed = freetype-2.4.8-cross-compile.patch... (2 Replies)
Discussion started by: alexzander18
2 Replies

4. Shell Programming and Scripting

Convert few columns to rows

Hi! Does anybody help me in converting following data: INPUT looks like this: 20. 100. 30 200. 40. 400. 50. 100. 60. 200. 70. 400. 80. 200. 150. 210. 30. 100. OUTPUT should look like this: 20. 100. 30 200. 40. 400. 50. 100. 60. 200. 70.... (5 Replies)
Discussion started by: lovelinux
5 Replies

5. Shell Programming and Scripting

Perl script to get info from specific rows & columns (.xls file)

Hi all, I want to read some specific rows & columns in the .xls file with my script to get the data to be manipulated. Now, I can read the .xls file correctly & i can go to the specific sheet that I want but i have a problem to specify the specific rows & columns. I mean, I want to get the info... (0 Replies)
Discussion started by: Yohannita
0 Replies

6. Shell Programming and Scripting

convert rows to columns

hi, i have the file as below: abc def ghi jkl i want the output as abc,def,ghi,jki please reply, Thanks (4 Replies)
Discussion started by: namitai
4 Replies

7. Shell Programming and Scripting

perl script to print to values in rows and columns

Hi guys I want to print the values by using this script but its giving the no of rows and columns as input instead of values Would you plz help me on this FILE- chr1.txt 1981 1 1971 1 1961 1 1941 1 perl script #!/usr/bin/perl -w $infile1 = 'chr1.txt'; $outfile3 = 'out3.txt'; ... (3 Replies)
Discussion started by: nogu0001
3 Replies

8. Shell Programming and Scripting

convert rows into columns

Hi guys Could anyone advise me how to convert my rows into columns from a file My file would be similar to this: A11 A12 A13 A14 A15 ... A1n A21 A22 A23 A31 A41 A51 ... Am1 Am2 Am3 Am4 Am5 ... Amn The number of rows is not the same to the number of columns Thanks in advance (2 Replies)
Discussion started by: loperam
2 Replies

9. Shell Programming and Scripting

how to convert columns to rows

Hi, I need a shell script for below requirement Input file P1 - 173310 P2 - 173476 P3 - 173230 P4 - 172737 P1 - 173546 P2 - 173765 P3 - 173876 P4 - 172989 Out put file P1 173310 173546 P2 173476 173765 P3 173230 173876 P4 172737 172989 Suresh (6 Replies)
Discussion started by: suresh3566
6 Replies

10. Shell Programming and Scripting

shell script required to convert rows to columns

Hi Friends, I have a log file as below siteid = HYD spc = 100 rset = RS_D_M siteid = DEL spc = 200 rset = RS_K_L siteid = DEL2 spc = 210 rset = RS_D_M Now I need a output like column wise as below. siteid SPC rset HYD 100 RS_D_M (2 Replies)
Discussion started by: suresh3566
2 Replies
Login or Register to Ask a Question