Change many columns position/order


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change many columns position/order
# 1  
Old 04-09-2010
Change many columns position/order

Hi everyone,

Please some help over here. (I´m using cygwing)

I have files with 40 columns and 2000 lines in average. I´m trying to change the order position as follow.


Original columns position:
Code:
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40

Desired columns position:
Code:
1,10,21,5,9,7,32,18,20,37,15,2,3,4,6,8,11,12,13,14,16,17,19,22,23,24,25,26,27,28,29,30,31,33,34,35,36,38,39,40

I was trying using the follow awk script:
Code:
awk -F"," '{print $1,$10,$21,$5,$9,$7,$32,$18,$20,$37,$15,$2,$3,$4,$6,$8,$11,$12,$13,$14,$16,$17,$19,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$33,$34,$35,$36,$38,$39,$40'}' infile

but doesn´t work only if I reduce the script to the first 11 columns, as follow:

Code:
awk -F"," '{print $1,$10,$21,$5,$9,$7,$32,$18,$20,$37,$15}' infile

Somebody knows what is wrong with my script?

How can be a best way to change all 40 columns position?

Thanks in advance for any help.

Regards.
# 2  
Old 04-09-2010
Hi.

You have an extraneous single quote at:
Code:
$40'}' infile

I removed the one just before the closing curly brace and it worked for me.

Note that if you need the comma to be a separator in the output, you will need to change the value of variable OFS ... cheers, drl
# 3  
Old 04-09-2010
It works on my Solaris box with all kinds of awk's. Is the extra ' at the end of your awk statement a typo or the cause of your problem: ... $40'}' ...
# 4  
Old 04-09-2010
Java

Hey, many thanks guys!. Yes, a clear typoSmilie. I didn´t see the extra '.

Thanks, thanks, thanks.

Best regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delete columns with a specific title XXX, where the position change in each file

Goodmorning, I know how to cut a string and a column, and how to find a word. I have a file with over 100 columns. All columns have a title in the first line. I have to delete all columns with the XXX title. I can't use cut -f because the position of XXX columns change in each file, and in... (14 Replies)
Discussion started by: echo manolis
14 Replies

2. Shell Programming and Scripting

Re-order columns

I am trying to reorder columns based on a value in a field in the attached file. Thank you :). If $11 is not equal to "unknown", then in a new text file $4, $11, $7, $13 For example, row 24 $11 is not unknown A_16_P39089234, chr10:76765556-76765615, KAT6B, 0.9337 (5 Replies)
Discussion started by: cmccabe
5 Replies

3. UNIX for Dummies Questions & Answers

change service order

hi guys I have a service that depends on some shares (NFS shares ) that need to be mounted before before the service start so the service-app finds the NFS shares and starts correctly... I am confused here this is what I found but I am not sure what to do in order to change it BTW is Suse... (2 Replies)
Discussion started by: karlochacon
2 Replies

4. Shell Programming and Scripting

change order

I have inside a file 22 25 80 111 631 694 861 875 I need this in the form using awk or sed 22,25,80,111,631,694,861,875 (4 Replies)
Discussion started by: anil510
4 Replies

5. Shell Programming and Scripting

position change

Hi All, input file: echo "a|b|c|d|test|123|same" | awk 'BEGIN {FS=OFS="|"} {print $1,$2,$7,$4,$5,$6}' Got Output : a|b|same|d|test|123 This is the sample input file with less fields. But my original file contains more field values. How to replace last value to 3rd postion value. Because... (8 Replies)
Discussion started by: Jairaj
8 Replies

6. Shell Programming and Scripting

Change order

Good evening I have a file as below and want to change the order, as in the second column, sed awk Pearl Thanks aaaaaaaaaa bbbbbbbbb cccccccc aaaaaaaaaa bbbbbbbbb cccccccc aaaaaaaaaa cccccccc bbbbbbbbb aaaaaaaaaa cccccccc bbbbbbbbb (8 Replies)
Discussion started by: Novice-
8 Replies

7. Shell Programming and Scripting

Read columns from file by position

Hello , i have a fixed-length record file where each column has a specific position. how can retrive two or more column based on their positions in the file ? Thank you (5 Replies)
Discussion started by: alain.kazan
5 Replies

8. Shell Programming and Scripting

How to change a segment in a particular position

I need help in removing a leading zero in a particular position. For eg.: XYZ*04567472*0099*020091231*0123*0.12 In the above line, I want to replace "*0123" with "123" and "0.12" with ".12". I want to remove the leading zero only in position number 4 and 5 (the bolded segments) I was able... (10 Replies)
Discussion started by: ananthmm
10 Replies

9. UNIX for Dummies Questions & Answers

How to change the order of a string ?

Hi , I want to change the order of a string using sed command . Is it possible ? $echo "abc123xyz" | sed 's/\()*\) \(*\)/\2\1/' abc123xyz $ echo "abc123xyz" |sed 's/\()*\) \(*\) \()*\)/\2\1\3/' abc123xyz I want to change the string , abc123xyz as xyz123abc . Is it... (5 Replies)
Discussion started by: rajavu
5 Replies

10. UNIX for Dummies Questions & Answers

Changing the order of columns in a script

I have the following script: (echo -n `curl http://www.example.com/scores.txt | grep mylocation`; date +%Y%m%d%H%M%S) >> myscores.txt This script works fine, except that it places the timestamp at the end of the file myscores.txt. How do add the timestamp as the first column and then a tab and... (4 Replies)
Discussion started by: figaro
4 Replies
Login or Register to Ask a Question