rearrange columns using awk or cut command

后端 未结 4 2079
闹比i
闹比i 2020-12-12 01:34

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.         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-12 02:04

    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.

提交回复
热议问题