col

col 命令可以用来转换tab 与空格。

image.png
操作示例

  1. # 查看 /etc/protocols 中的不可见字符,可以看到很多 ^I ,这其实就是 Tab 转义成可见字符的符号
  2. $ cat -A /etc/protocols
  3. # 使用 col -x 将 /etc/protocols 中的 Tab 转换为空格,然后再使用 cat 查看,你发现 ^I 不见了
  4. $ cat /etc/protocols | col -x | cat -A

join

和数据库或者其他编程语言中的join 非常类似,就是将文件通过相同的行连接在一起。

join [option]... file1 file2

image.png

$ cd /home/shiyanlou
# 创建两个文件
$ echo '1 hello' > file1
$ echo '1 shiyanlou' > file2
$ join file1 file2
# 将/etc/passwd与/etc/shadow两个文件合并,指定以':'作为分隔符
$ sudo join -t':' /etc/passwd /etc/shadow
# 将/etc/passwd与/etc/group两个文件合并,指定以':'作为分隔符, 分别比对第4和第3个字段
$ sudo join -t':' -1 4 /etc/passwd -2 3 /etc/group

# 也就是以: 分隔passwd 与group 中的内容,其中取前者的第四段和后者的第三段合并

image.png

paste

image.png

$ echo hello > file1
$ echo shiyanlou > file2
$ echo www.lanqiao.cn > file3
$ paste -d ':' file1 file2 file3
$ paste -s file1 file2 file3

另外,还可以使用 - ,直接对传递的文本本身进行分段处理。
image.png
若干个 - 则将文本按照设定分隔符分隔为若干列。

练习

image.png