我正在尝试交换“点击数”和“域”列。
cat filename
输出:
Hits Domains
204492 greenlivingasc.in
37102 smcracks.uk
17410 bahriains.org
10975 cmhlahore.co.uk
10342 albps.in
9541 mediabites.co.uk
但是当我使用以下命令交换列时,awk '{ print $2 " " $1}' filename
它输出未对齐的格式:
Domains Hits
greenlivingasc.in 204492
smcracks.uk 37102
bahriains.org 17410
cmhlahore.co.uk 10975
albps.in 10342
mediabites.co.uk 9541
我想在交换后得到正确的对齐。
答案1
您应该将其输入column -t
结果,例如:
awk '{ print $2 " " $1}' file.txt | column -t
答案2
column
像mau help这样的解决方案:
:~$ column -t 123
Hits Domains
204492 greenlivingasc.in
37102 smcracks.uk
17410 bahriains.org
10975 cmhlahore.co.uk
10342 albps.in
9541 mediabites.co.uk
另一种方法是使用expand
:
:~$ cat 123 | expand --tabs=10
Hits Domains
204492 greenlivingasc.in
37102 smcracks.uk
17410 bahriains.org
10975 cmhlahore.co.uk
10342 albps.in
9541 mediabites.co.uk
或者尝试更改 awk 表达式:
cat 123 | awk -v cols=1 '{printf("%-44s",$0)} NR%cols==0 {print ""} END {print ""}'