column格式化输出

规则的打印输出

之前遇到过这个格式化输出的问题,之间用echo或者printf很难达到想要的效果,因为它无法实现字符长度不同的列保持相同的宽度。不过后来使用awk的printf解决了这个问题,可以实现不同宽度的列对齐,但是写起来听麻烦的。今天发现了一个新的命令column,很简单的一个命令完美的解决了问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
--- Code/shell » head ss.final 
sslocal -s 139.28.235.243 -p 5911 -k lncn.org r6* -m rc4 -l 1080
sslocal -s 46.17.45.194 -p 5922 -k lncn.org 2gt -m rc4 -l 1080
sslocal -s 45.136.244.204 -p 5922 -k lncn.org 2gt -m rc4 -l 1080
sslocal -s 45.12.109.134 -p 5922 -k lncn.org 5tb -m rc4 -l 1080
sslocal -s 85.117.234.98 -p 5922 -k lncn.org t2j -m rc4 -l 1080
sslocal -s 45.147.201.168 -p 5922 -k lncn.org t2j -m rc4 -l 1080
sslocal -s 45.82.255.115 -p 5922 -k lncn.org 5tb -m rc4 -l 1080
sslocal -s 45.135.134.205 -p 5922 -k lncn.org 6tw -m rc4 -l 1080
sslocal -s 45.144.2.51 -p 5922 -k lncn.org t2j -m rc4 -l 1080
sslocal -s 45.144.2.186 -p 5922 -k lncn.org t2j -m rc4 -l 1080

# 简单的格式化,以table的格式打印 -t
--- Code/shell » head ss.final | column -t
sslocal -s 139.28.235.243 -p 5911 -k lncn.org r6* -m rc4 -l 1080
sslocal -s 46.17.45.194 -p 5922 -k lncn.org 2gt -m rc4 -l 1080
sslocal -s 45.136.244.204 -p 5922 -k lncn.org 2gt -m rc4 -l 1080
sslocal -s 45.12.109.134 -p 5922 -k lncn.org 5tb -m rc4 -l 1080
sslocal -s 85.117.234.98 -p 5922 -k lncn.org t2j -m rc4 -l 1080
sslocal -s 45.147.201.168 -p 5922 -k lncn.org t2j -m rc4 -l 1080
sslocal -s 45.82.255.115 -p 5922 -k lncn.org 5tb -m rc4 -l 1080
sslocal -s 45.135.134.205 -p 5922 -k lncn.org 6tw -m rc4 -l 1080
sslocal -s 45.144.2.51 -p 5922 -k lncn.org t2j -m rc4 -l 1080
sslocal -s 45.144.2.186 -p 5922 -k lncn.org t2j -m rc4 -l 1080

# 指定分隔符 -s
--- ~/Desktop » cat passwd
root:x:0:0::/root:/bin/bash
bin:x:1:1::/:/usr/bin/nologin
daemon:x:2:2::/:/usr/bin/nologin
mail:x:8:12::/var/spool/mail:/usr/bin/nologin

--- ~/Desktop » column -t -s: passwd
root x 0 0 /root /bin/bash
bin x 1 1 / /usr/bin/nologin
daemon x 2 2 / /usr/bin/nologin
mail x 8 12 /var/spool/mail /usr/bin/nologin

# 忽略某些列 -H
--- Code/shell » head ss.final | column -t -H 1,2,3
-p 5911 -k lncn.org r6* -m rc4 -l 1080
-p 5922 -k lncn.org 2gt -m rc4 -l 1080
-p 5922 -k lncn.org 2gt -m rc4 -l 1080
-p 5922 -k lncn.org 5tb -m rc4 -l 1080
-p 5922 -k lncn.org t2j -m rc4 -l 1080
-p 5922 -k lncn.org t2j -m rc4 -l 1080
-p 5922 -k lncn.org 5tb -m rc4 -l 1080
-p 5922 -k lncn.org 6tw -m rc4 -l 1080
-p 5922 -k lncn.org t2j -m rc4 -l 1080
-p 5922 -k lncn.org t2j -m rc4 -l 1080

# 按指定的列顺序 -O
--- Code/shell » head ss.final | column -t -O 12,3,2,1
1080 139.28.235.243 -s sslocal -p 5911 -k lncn.org r6* -m rc4 -l
1080 46.17.45.194 -s sslocal -p 5922 -k lncn.org 2gt -m rc4 -l
1080 45.136.244.204 -s sslocal -p 5922 -k lncn.org 2gt -m rc4 -l
1080 45.12.109.134 -s sslocal -p 5922 -k lncn.org 5tb -m rc4 -l
1080 85.117.234.98 -s sslocal -p 5922 -k lncn.org t2j -m rc4 -l
1080 45.147.201.168 -s sslocal -p 5922 -k lncn.org t2j -m rc4 -l
1080 45.82.255.115 -s sslocal -p 5922 -k lncn.org 5tb -m rc4 -l
1080 45.135.134.205 -s sslocal -p 5922 -k lncn.org 6tw -m rc4 -l
1080 45.144.2.51 -s sslocal -p 5922 -k lncn.org t2j -m rc4 -l
1080 45.144.2.186 -s sslocal -p 5922 -k lncn.org t2j -m rc4 -l

# 指定某列右对齐 -R
--- Code/shell » head ss.final | column -t -R 3
sslocal -s 139.28.235.243 -p 5911 -k lncn.org r6* -m rc4 -l 1080
sslocal -s 46.17.45.194 -p 5922 -k lncn.org 2gt -m rc4 -l 1080
sslocal -s 45.136.244.204 -p 5922 -k lncn.org 2gt -m rc4 -l 1080
sslocal -s 45.12.109.134 -p 5922 -k lncn.org 5tb -m rc4 -l 1080
sslocal -s 85.117.234.98 -p 5922 -k lncn.org t2j -m rc4 -l 1080
sslocal -s 45.147.201.168 -p 5922 -k lncn.org t2j -m rc4 -l 1080
sslocal -s 45.82.255.115 -p 5922 -k lncn.org 5tb -m rc4 -l 1080
sslocal -s 45.135.134.205 -p 5922 -k lncn.org 6tw -m rc4 -l 1080
sslocal -s 45.144.2.51 -p 5922 -k lncn.org t2j -m rc4 -l 1080
sslocal -s 45.144.2.186 -p 5922 -k lncn.org t2j -m rc4 -l 1080

# 指定输出分割符 -o
--- Code/shell » head ss.final | column -t -o '| |'
sslocal| |-s| |139.28.235.243| |-p| |5911| |-k| |lncn.org| |r6*| |-m| |rc4| |-l| |1080
sslocal| |-s| |46.17.45.194 | |-p| |5922| |-k| |lncn.org| |2gt| |-m| |rc4| |-l| |1080
sslocal| |-s| |45.136.244.204| |-p| |5922| |-k| |lncn.org| |2gt| |-m| |rc4| |-l| |1080
sslocal| |-s| |45.12.109.134 | |-p| |5922| |-k| |lncn.org| |5tb| |-m| |rc4| |-l| |1080
sslocal| |-s| |85.117.234.98 | |-p| |5922| |-k| |lncn.org| |t2j| |-m| |rc4| |-l| |1080
sslocal| |-s| |45.147.201.168| |-p| |5922| |-k| |lncn.org| |t2j| |-m| |rc4| |-l| |1080
sslocal| |-s| |45.82.255.115 | |-p| |5922| |-k| |lncn.org| |5tb| |-m| |rc4| |-l| |1080
sslocal| |-s| |45.135.134.205| |-p| |5922| |-k| |lncn.org| |6tw| |-m| |rc4| |-l| |1080
sslocal| |-s| |45.144.2.51 | |-p| |5922| |-k| |lncn.org| |t2j| |-m| |rc4| |-l| |1080
sslocal| |-s| |45.144.2.186 | |-p| |5922| |-k| |lncn.org| |t2j| |-m| |rc4| |-l| |1080

# 结合awk在行首行尾添加
--- Code/shell » head ss.final | column -t -o '| |' | awk '{print "|" $0 "|"}'
|sslocal| |-s| |139.28.235.243| |-p| |5911| |-k| |lncn.org| |r6*| |-m| |rc4| |-l| |1080|
|sslocal| |-s| |46.17.45.194 | |-p| |5922| |-k| |lncn.org| |2gt| |-m| |rc4| |-l| |1080|
|sslocal| |-s| |45.136.244.204| |-p| |5922| |-k| |lncn.org| |2gt| |-m| |rc4| |-l| |1080|
|sslocal| |-s| |45.12.109.134 | |-p| |5922| |-k| |lncn.org| |5tb| |-m| |rc4| |-l| |1080|
|sslocal| |-s| |85.117.234.98 | |-p| |5922| |-k| |lncn.org| |t2j| |-m| |rc4| |-l| |1080|
|sslocal| |-s| |45.147.201.168| |-p| |5922| |-k| |lncn.org| |t2j| |-m| |rc4| |-l| |1080|
|sslocal| |-s| |45.82.255.115 | |-p| |5922| |-k| |lncn.org| |5tb| |-m| |rc4| |-l| |1080|
|sslocal| |-s| |45.135.134.205| |-p| |5922| |-k| |lncn.org| |6tw| |-m| |rc4| |-l| |1080|
|sslocal| |-s| |45.144.2.51 | |-p| |5922| |-k| |lncn.org| |t2j| |-m| |rc4| |-l| |1080|
|sslocal| |-s| |45.144.2.186 | |-p| |5922| |-k| |lncn.org| |t2j| |-m| |rc4| |-l| |1080|