0

I have a file with a very large number of columns (basically several thousand sets of threes) with three special columns (Chr and Position and Name) at the end.

I want to move these final three columns to the front of the file, so that that columns become Name Chr Position and then the file continues with the trios.

I think this might be possible with awk, but I don't know enough about how awk works to do it!

Sample input:

Gene1.GType Gene1.X    Gene1.Y ....ending in GeneN.Y     Chr     Position        Name

Desired Output:

Name    Chr     Position        (Gene1.GType Gene1.X     Gene1.Y ) x n samples
4

2 回答 2

1

I think the below example does more or less what you want.

$ cat file
A B C D E F G Chr Position Name
1 2 3 4 5 6 7 8 9 10
$ cat process.awk 
{
  printf $(NF-2)" "$(NF-1)" "$NF
  for( i=1; i<NF-2; i++)
  {
   printf " "$i
  }
  print " "
}
$ awk -f process.awk file 
Chr Position Name A B C D E F G 
8 9 10 1 2 3 4 5 6 7

NF in awk denotes the number of field on a row.

于 2013-06-25T07:41:12.040 回答
0

one liner:

awk '{ Chr=$(NF-2) ; Position=$(NF-1) ; Name=$NF ; $(NF-2)=$(NF-1)=$NF="" ; print Name, Chr, Position, $0 }' file
于 2013-06-26T04:17:58.967 回答