gfo*_*e89 1 bash shell awk sed
我有一个文件,我想重新安排....
输入文件:
sublat
16 0.04 0.051
32 0.04 0.050
16 0.06 0.055
32 0.06 0.054
c2dotc2
16 0.04 0.464
32 0.04 0.624
16 0.06 0.505
32 0.06 0.743
Run Code Online (Sandbox Code Playgroud)
输出文件:
A B sublat c2dotc2
0.04 16 0.051 0.464
0.04 32 0.050 0.624
0.06 16 0.055 0.624
0.06 32 0.054 0.743
Run Code Online (Sandbox Code Playgroud)
我如何使用awk,sed实现这一目标?
这个awk脚本适合您的要求:
awk 'BEGIN{OFS="\t";h="A\tB"}
NF==1{h=h OFS $0;next}
{x=$2OFS$1;r[x]=!r[x]?$3:r[x]OFS$3}
END{print h;n=asorti(r,d);for(i=1;i<=n;i++)print d[i],r[d[i]]}' file
Run Code Online (Sandbox Code Playgroud)
测试您的数据:
kent$ cat file
sublat
16 0.04 0.051
32 0.04 0.050
16 0.06 0.055
32 0.06 0.054
c2dotc2
16 0.04 0.464
32 0.04 0.624
16 0.06 0.505
32 0.06 0.743
kent$ awk 'BEGIN{OFS="\t";h="A\tB"}
NF==1{h=h OFS $0;next}
{x=$2OFS$1;r[x]=!r[x]?$3:r[x]OFS$3}
END{print h;n=asorti(r,d);for(i=1;i<=n;i++)print d[i],r[d[i]]}' file
A B sublat c2dotc2
0.04 16 0.051 0.464
0.04 32 0.050 0.624
0.06 16 0.055 0.505
0.06 32 0.054 0.743
Run Code Online (Sandbox Code Playgroud)