在我的数据文件中,有一些我感兴趣的专栏。因此我使用awk只打印出该列(awk'{print $ 4}'),并使用“if”来消除条件。但是,我无法弄清楚如何将该列的每第n行转置为新行。
输入:
1
2
3
4
5
6
7
8
9
期望的输出:
1 4 7
2 5 8
3 6 9
我已经检查了其他解决方案并试过但没有一个给了我想要的东西。如果有人能帮助我,我将不胜感激。
你也可以在这里使用pr
$ seq 9 | pr -3ts' '
1 4 7
2 5 8
3 6 9
$ seq 9 | pr -5ts' '
1 3 5 7 9
2 4 6 8
其中数字表示您需要多少列,s
选项允许指定列之间的分隔符
使用awk:
$ seq 9 |
awk ' {
i=((i=NR%3)?i:3) # index to hash a
a[i]=a[i] (a[i]==""?"":" ") $1 # space separate items to a[i]
}
END {
for(i=1;i<=3;i++) # from 1 to 3 (yes, hardcoded)
print a[i] # output
}'
输出:
1 4 7
2 5 8
3 6 9
来自autogen包的columns
程序可以做到这一点,例如:
seq 9 | columns --by-column -w1 -c3
输出:
1 4 7
2 5 8
3 6 9