使用column格式化输出
source link: https://www.lujun9972.win/blog/2018/02/08/%E4%BD%BF%E7%94%A8column%E6%A0%BC%E5%BC%8F%E5%8C%96%E8%BE%93%E5%87%BA/index.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
使用column格式化输出
column是一个将输入格式化成多列格式来显示的工具。
举个例子,有一个num.txt的文件包含了从00到24共25个数字,每行一个数字
seq -w 0 24 >/tmp/num.txt
现在我想把它变成5列,每列20行,可以这样做
column -c 40 /tmp/num.txt
00 05 10 15 20 01 06 11 16 21 02 07 12 17 22 03 08 13 18 23 04 09 14 19 24
注意这里的 -c 40
表示字符宽度为40,那么40这个数字是怎么算出来的呢?
column使用TAB来作为列与列之间的分割,因此在内容长度小于8的情况下,每一列占用8个字符,5列就是 5*8=40
你会发现,我们产生的顺序是先排第一列(00-04),再排第二列(05-09),再排第三列(10-14)...,依次这么排下去
但若我想要先排第一行,再排第二行...,要怎么办呢?可以使用 -x
选项
column -x -c 40 /tmp/num.txt
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
column还有一个广为人知的功能就是通过 -t
选项把一个已经是表格的内容排列整齐,比如网上经常看到的例子
grep -v '#' /etc/fstab |column -t
UUID=076e5e76-1624-4b38-a80c-7c822f171f56 / ext4 rw,relatime,data=ordered,acl 0 1 UUID=c2cf24f1-0ea3-4fe4-a8d3-5e2629bae9f5 swap swap defaults 0 0 UUID=a31ff6e0-991a-497b-a2d3-4c3dafc62a90 /home ext4 rw,relatime,data=ordered,acl 0 2
-t
选项,还经常与 -s 分隔符
在一起使用,这里 -s
用来指明表格中的分隔符,比如
column -s ",|" -t <<EOF 1,|2,|3 A,|B,|C 你好,|我好,|它好 EOF
1 2 3 A B C 你好 我好 它好
可以看出,这里的分隔符可以是多个字符组成的序列
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK