rearrange columns using awk or cut command

awk, cut

Solution

try this awk one-liner:

awk '{$3=$NF OFS $3;$NF=""}7' file

this is moving the last col to the 3rd col. if you have 1000, then it does it with 1000th col.

EDIT

if the file is tab-delimited, you could try:

awk -F'\t' -v OFS="\t" '{$3=$NF OFS $3;$NF=""}7' file

EDIT2

add an example:

kent$  seq 20|paste -s -d'\t'                              
1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  19  20

kent$  seq 20|paste -s -d'\t'|awk -F'\t' -v OFS="\t" '{$3=$NF OFS $3;$NF=""}7'
1   2   20  3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  19  

EDIT3

You didn't give any input example. so assume you don't have empty columns in original file. (no continuous multi-tabs):

kent$  seq 20|paste -s -d'\t'|awk -F'\t'  -v OFS="\t" '{$3=$10 FS $11 FS $3;$10=$11="";gsub(/\t+/,"\t")}7'
1       2       10      11      3       4       5       6       7       8       9       12      13      14      15      16      17      18      19      20

After all we could print those fields in a loop.

Problem

I have large file with 1000 columns. I want to rearrange so that last column should be the 3rd column. FOr this i have used, ``` cut -f1-2,1000,3- file > out.txt ``` But this does not change the order. Could anyone help using cut or awk? Also, I want to rearrange columns 10 and 11 as shown below: Example: ``` 1 10 11 2 3 4 5 6 7 8 9 12 13 14 15 16 17 18 19 20 ```

Original source

Related problems