rearrange the column names with comma as column delimiter

Hi,
I am new to shell scripting, i have requirement can any one help me out in this regrads,
in directory i have file like invoice1.txt, invoice2.txt in each file i have fixed number of columns, 62 in number but they are randomly arranged.like
for first file invoice1.txt can have columns
col1,col2,col3,col4,col5......col62
secon file can have
col1,col3,col2.....................(upto62cols)
first line of the file has column names.
i need to arrange the columns in fixed columns regard less of the order the files are:
col1,col3,col5...................col61,col2,col4............col62(total column 62)
can any one suggest me script to run on AIX platform.

Hmm..., simple the solution might be, but difficult to understand what you want is.

Could you post portion of the actual relevant files, and an example of the real expected output?

example file:
col1,col2,col3,col4,col5......col62
1,a1,a2,a3,a4...................a61
2,b1,b2,b3,b4...................b61

expected file lets say:
Col62,col61,col60...................col1
a61,a60.................................1
b61,b60.................................2

Hi

awk '{for(i=NF;i!=0;i--)if(i==1)print $i;else printf("%s,", $i);}' FS=,  file

Guru.

Small variation:

awk -F, '{for(i=NF;i>1;i--) printf $i FS; print $1}' infile
$ 
$ # Example file
$ cat f0.example
col1,col2,col3,col4,col5
a1,a2,a3,a4,a5
b1,b2,b3,b4,b5
$ 
$ # File with shuffled columns
$ cat f0.shuffled
col5,col3,col2,col4,col1
a5,a3,a2,a4,a1
b5,b3,b2,b4,b1
c5,c3,c2,c4,c1
d5,d3,d2,d4,d1
e5,e3,e2,e4,e1
$ 
$ # Run the Perl script, passing the example file and shuffled data file.
$ # The script reshuffles the header and data of f0.shuffled so that
$ # it matches the order of the header columns of f0.example.
$ perl -lne 'if ($.==1 && $#ARGV == 0) {
             for $k (split/,/) {$x{$k} = $i++}
           } elsif ($.==1) {
             for $k (split/,/) {push @a,$x{$k}; $b[$x{$k}] = $k}
             print join ",",@b; @b=();
           } elsif ($#ARGV == -1) {$i = 0;
             for $k (split/,/) {$n = $a[$i]; $b[$n] = $k; $i++}
             print join ",",@b; @b = ();
           }
           close ARGV if eof;
          ' f0.example f0.shuffled
col1,col2,col3,col4,col5
a1,a2,a3,a4,a5
b1,b2,b3,b4,b5
c1,c2,c3,c4,c5
d1,d2,d3,d4,d5
e1,e2,e3,e4,e5
$ 
$ # Another file with (differently) shuffled columns
$ cat f1.shuffled
col3,col5,col4,col1,col2
a3,a5,a4,a1,a2
b3,b5,b4,b1,b2
$ 
$ # Run the Perl script, passing the example file and shuffled data file.
$ # The script reshuffles the header and data of f1.shuffled so that
$ # it matches the order of the header columns of f0.example.
$ perl -lne 'if ($.==1 && $#ARGV == 0) {
             for $k (split/,/) {$x{$k} = $i++}
           } elsif ($.==1) {
             for $k (split/,/) {push @a,$x{$k}; $b[$x{$k}] = $k}
             print join ",",@b; @b=();
           } elsif ($#ARGV == -1) {$i = 0;
             for $k (split/,/) {$n = $a[$i]; $b[$n] = $k; $i++}
             print join ",",@b; @b = ();
           }
           close ARGV if eof;
          ' f0.example f1.shuffled
col1,col2,col3,col4,col5
a1,a2,a3,a4,a5
b1,b2,b3,b4,b5
$ 
$ 
$ 

tyler_durden