重新用awk或剪切命令列 [英] rearrange columns using awk or cut command
问题描述
我有1000列的大文件。我想重新排列,使最后一栏应该是第3列。为此,我都用过,
削减-f1-2,1000,3-文件> out.txt
但是,这并不改变顺序。
任何人都可以使用剪切或AWK帮助?
另外,我想重新排列列10和11,如下图所示:
例如:
1 10 11 2 3 4 5 6 7 8 9 12 13 14 15 16 17 18 19 20
试试这个AWK一行代码:
的awk'{$ 3 = $ NF OFS $ 3; $ NF =} 7'文件
这是移动的最后一个山坳到3山坳。如果你有1000的话,就加进了第1000关口。
修改
如果该文件是制表符分隔,你可以尝试:
的awk -F'\\ t'-v OFS =\\ t的{$ 3 = $ NF OFS $ 3; $ NF =} 7'文件
EDIT2
添加一个例子:
肯特$ 20序列|贴-s -d'\\ t'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20肯特$ 20序列|贴-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
您没有给任何输入的例子。所以假设你没有在原始文件空列。 (无连续多标签):
肯特$ 20序列|贴-s -d'\\ t'| awk的-F'\\ t'-v OFS =\\ t的{$ 3 = $ 10fs的$ 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
毕竟,我们可以在一个循环打印这些领域。
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
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.
这篇关于重新用awk或剪切命令列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!