Linux命令——column

参考:Viewing Linux output in columns

功能

column命令把他的输入格式化多列显示。输入可以是文件,也可以是标准输入。

列优先,从左到右

显示的时候首先填满最左列,然后右列,顺序如下

1 4 7 10

2 5 8 11

3 6 9 12

测试文件

$ cat RR
I've been working on the railroad
All the live long day
I've been working on the railroad
Just to pass the time away

Can't you hear the whistle blowing
Rise up so early in the morn
Can't you hear the whistle blowing
Dinah, blow your horn

Dinah, won't you blow
Dinah, won't you blow
Dinah, won't you blow your horn
Dinah, won't you blow,
Dinah, won't you blow,
Dinah, won't you blow your horn
View Code

显示结果

$ column RR
I've been working on the railroad       Dinah, blow your horn
All the live long day                   Dinah, won't you blow
I've been working on the railroad       Dinah, won't you blow
Just to pass the time away              Dinah, won't you blow your horn
Can't you hear the whistle blowing      Dinah, won't you blow,
Rise up so early in the morn            Dinah, won't you blow,
Can't you hear the whistle blowing      Dinah, won't you blow your horn
View Code

行优先,从上到下

使用-x参数

$ cat RR
I've been working on the railroad
All the live long day
I've been working on the railroad
Just to pass the time away

Can't you hear the whistle blowing
Rise up so early in the morn
Can't you hear the whistle blowing
Dinah, blow your horn

Dinah, won't you blow
Dinah, won't you blow
Dinah, won't you blow your horn
Dinah, won't you blow,
Dinah, won't you blow,
Dinah, won't you blow your horn
$ column -x RR
I've been working on the railroad       All the live long day
I've been working on the railroad       Just to pass the time away
Can't you hear the whistle blowing      Rise up so early in the morn
Can't you hear the whistle blowing      Dinah, blow your horn
Dinah won't you blow                    Dinah won't you blow
Dinah, won't you blow your horn         Dinah, won't you blow,
Dinah, won't you blow,                  Dinah, won't you blow your horn
View Code

保留空行

使用-e参数

$ cat RR
I've been working on the railroad
All the live long day
I've been working on the railroad
Just to pass the time away

Can't you hear the whistle blowing
Rise up so early in the morn
Can't you hear the whistle blowing
Dinah, blow your horn

Dinah, won't you blow
Dinah, won't you blow
Dinah, won't you blow your horn
Dinah, won't you blow,
Dinah, won't you blow,
Dinah, won't you blow your horn
$ column -e RR
I've been working on the railroad       Dinah, blow your horn
All the live long day
I've been working on the railroad       Dinah, won't you blow
Just to pass the time away              Dinah, won't you blow
                                        Dinah, won't you blow your horn
Can't you hear the whistle blowing      Dinah, won't you blow,
Rise up so early in the morn            Dinah, won't you blow,
Can't you hear the whistle blowing      Dinah, won't you blow your horn
View Code

指定分隔符

默认的分隔符是空格,可以使用-s指定分隔符

$ cat abc
a:b:c:d:e
$ column -t -s: abc
a  b  c  d  e
View Code

把行创建出列表

上面指定分隔符的时候有个参数-t。-t用于根据-s指定的分隔符(默认为空格)将一行创建出几列。

这个参数经常用于查看mount输出。mount输出内容比较杂,每一行包含很多信息,信息之间以空格分隔

[root@localhost bin]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=2001952k,nr_inodes=500488,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,seclabel,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,net_prio,net_cls)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,devices)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,cpuset)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,hugetlb)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,memory)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,perf_event)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,freezer)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,blkio)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,cpuacct,cpu)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,pids)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/mapper/centos-root on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
selinuxfs on /sys/fs/selinux type selinuxfs (rw,relatime)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=34,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=14124)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel)
mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
/dev/mapper/centos-home on /home type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=402844k,mode=700)
View Code
[root@localhost bin]# mount | column -t
sysfs                    on  /sys                             type  sysfs       (rw,nosuid,nodev,noexec,relatime,seclabel)
proc                     on  /proc                            type  proc        (rw,nosuid,nodev,noexec,relatime)
devtmpfs                 on  /dev                             type  devtmpfs    (rw,nosuid,seclabel,size=2001952k,nr_inodes=500488,mode=755)
securityfs               on  /sys/kernel/security             type  securityfs  (rw,nosuid,nodev,noexec,relatime)
tmpfs                    on  /dev/shm                         type  tmpfs       (rw,nosuid,nodev,seclabel)
devpts                   on  /dev/pts                         type  devpts      (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000)
tmpfs                    on  /run                             type  tmpfs       (rw,nosuid,nodev,seclabel,mode=755)
tmpfs                    on  /sys/fs/cgroup                   type  tmpfs       (ro,nosuid,nodev,noexec,seclabel,mode=755)
cgroup                   on  /sys/fs/cgroup/systemd           type  cgroup      (rw,nosuid,nodev,noexec,relatime,seclabel,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore                   on  /sys/fs/pstore                   type  pstore      (rw,nosuid,nodev,noexec,relatime)
cgroup                   on  /sys/fs/cgroup/net_cls,net_prio  type  cgroup      (rw,nosuid,nodev,noexec,relatime,seclabel,net_prio,net_cls)
cgroup                   on  /sys/fs/cgroup/devices           type  cgroup      (rw,nosuid,nodev,noexec,relatime,seclabel,devices)
cgroup                   on  /sys/fs/cgroup/cpuset            type  cgroup      (rw,nosuid,nodev,noexec,relatime,seclabel,cpuset)
cgroup                   on  /sys/fs/cgroup/hugetlb           type  cgroup      (rw,nosuid,nodev,noexec,relatime,seclabel,hugetlb)
cgroup                   on  /sys/fs/cgroup/memory            type  cgroup      (rw,nosuid,nodev,noexec,relatime,seclabel,memory)
cgroup                   on  /sys/fs/cgroup/perf_event        type  cgroup      (rw,nosuid,nodev,noexec,relatime,seclabel,perf_event)
cgroup                   on  /sys/fs/cgroup/freezer           type  cgroup      (rw,nosuid,nodev,noexec,relatime,seclabel,freezer)
cgroup                   on  /sys/fs/cgroup/blkio             type  cgroup      (rw,nosuid,nodev,noexec,relatime,seclabel,blkio)
cgroup                   on  /sys/fs/cgroup/cpu,cpuacct       type  cgroup      (rw,nosuid,nodev,noexec,relatime,seclabel,cpuacct,cpu)
cgroup                   on  /sys/fs/cgroup/pids              type  cgroup      (rw,nosuid,nodev,noexec,relatime,seclabel,pids)
configfs                 on  /sys/kernel/config               type  configfs    (rw,relatime)
/dev/mapper/centos-root  on  /                                type  xfs         (rw,relatime,seclabel,attr2,inode64,noquota)
selinuxfs                on  /sys/fs/selinux                  type  selinuxfs   (rw,relatime)
systemd-1                on  /proc/sys/fs/binfmt_misc         type  autofs      (rw,relatime,fd=34,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=14124)
hugetlbfs                on  /dev/hugepages                   type  hugetlbfs   (rw,relatime,seclabel)
mqueue                   on  /dev/mqueue                      type  mqueue      (rw,relatime,seclabel)
debugfs                  on  /sys/kernel/debug                type  debugfs     (rw,relatime)
/dev/mapper/centos-home  on  /home                            type  xfs         (rw,relatime,seclabel,attr2,inode64,noquota)
/dev/sda1                on  /boot                            type  xfs         (rw,relatime,seclabel,attr2,inode64,noquota)
tmpfs                    on  /run/user/0                      type  tmpfs       (rw,nosuid,nodev,relatime,seclabel,size=402844k,mode=700)
View Code

智障的设定

想想使用这个命令的场景,多数时候是因为输出结果一列显示太长了,多列显示能短一点。column虽然可以多列显示,但是你却不能控制他到底显示几列。这个设定相当智障。前面说到column显示规则,列优先,从左到右。他能显示的最大列数与你终端窗口允许显示最大列数有关,最左列显示不完才会再显示一列,以此类推。

pr解决了这个问题,参考:Linux命令——pr

不管你显示几列,每列宽度一样,如果某一行长度超过终端窗口宽度一半以上,那么你永远只能得到一列输出。更智障的设定

使用-c参数,你可以指定一列有多宽,不过貌似很少用得上。

原文地址:https://www.cnblogs.com/kelamoyujuzhen/p/10071501.html