Script - columns to rows


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script - columns to rows
# 1  
Old 01-11-2011
Script - columns to rows

Hi Everyone,

I have a file text.cvs, which is a file with columns from excel. The data are separeted with semicolon. The content of the file looks like
A B C D E F
1 3 3 4 3 3
2 2 1 2 5 2
5 6 1 1 2 1

Now I wish to write an script which writes the colums A, C and F in rows like
A 1 2 5
C 3 1 1
F 3 2 1
and save it in a new file newtext.cvs to be viewed in Excel.
Can you help me to write this script?
# 2  
Old 01-11-2011
If you're already using excel, just copy and paste (special) with transpose.
# 3  
Old 01-12-2011
I've posted only a simplified example. In the end I want to apply the script to transpose about 150 files with 30 columns each.
# 4  
Old 01-13-2011
Try something like...
Code:
$ cat file1
A;B;C;D;E;F
1;3;3;4;3;3
2;2;1;2;5;2
5;6;1;1;2;1

$ gawk -v o='A|C|F' '
   BEGIN {FS=OFS=";"}
   {
      for (i=1; i<=NF; i++)
         a[NR,i]=$i
   }
   NF>m {m=NF}
   END {
      for (i=1; i<=m; i++)
         if (a[1,i] ~ o)
            for (j=1; j<=NR; j++)
               printf "%s%s", a[j,i], (j==NR?ORS:OFS)
   }' file1 > file2

$ cat file2
A;1;2;5
C;3;1;1
F;3;2;1

$

# 5  
Old 01-13-2011
It works pretty well!! Thank you so much!!!
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

Help with shell script: selecting rows that have the same values in two columns

Hello, everyone I am beginner for shell programming. I want to print all lines that have the same values in first two columns data: a b 1 2 a a 3 4 b b 5 6 a b 4 6 what I expected is : a a 3 4 b b 5 6 but I searched for one hour in... (2 Replies)
Discussion started by: nengcheng
2 Replies

3. 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

4. UNIX for Dummies Questions & Answers

Writing a script to take the average of two columns every 3 rows

I have a dataset with 120 columns. I would like to write a script, that takes the average of every two columns, starting from columns 2 and 3, and moving consecutively in frames of 3 columns, all the way until the last column. The first column in the output file would be the averages of columns... (1 Reply)
Discussion started by: evelibertine
1 Replies

5. Shell Programming and Scripting

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 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 (3 Replies)
Discussion started by: parthmittal2007
3 Replies

6. Shell Programming and Scripting

Converting rows to columns using shell script

I have a script which converts rows to columns. file_name=$1 mailid=$2 #CREATE BACKUP OF ORIGINAL FILE #cp ${file_name}.xlsx ${file_name}_temp.xlsx #tr '\t' '|' < ${file_name}_temp.xlsx > ${file_name}_temp.csv #rm ${file_name}_temp.xlsx pivot_row=`head -1 ${file_name}` sed 1d... (3 Replies)
Discussion started by: andy538
3 Replies

7. Linux

help with columns and rows - script

Hi everyone, how can I convert a file with 3375 rows and 6 columns to a file with 1350 rows and 15 columns by using a script? Is it possible with awk or something like that? Any help is much appreciated. Thanks. D. (5 Replies)
Discussion started by: DKalfileritos
5 Replies

8. Shell Programming and Scripting

sql output from multiple rows and columns as variables in a script

This is for an Oracle journal import. I was using a pl/sql package and oracle API's. Oracle added invoker rights to their API's and now my package won't run. I didn't want to use their API's anyway. The only reason i was using pl/sql and the API's (just a package) was to utilize a cursor. How... (2 Replies)
Discussion started by: lmu
2 Replies

9. 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

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