Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
590 views
in Technique[技术] by (71.8m points)

rearrange columns using awk or cut command

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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'' -v OFS="" '{$3=$NF OFS $3;$NF=""}7' file

EDIT2

add an example:

kent$  seq 20|paste -s -d''                              
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''|awk -F'' -v OFS="" '{$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''|awk -F''  -v OFS="" '{$3=$10 FS $11 FS $3;$10=$11="";gsub(/+/,"")}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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...