**cat**命令能够显示或拼接文件内容,不过它的能力远不止如此。比如说,cat能够将标准输入数据与文件数据组合在一起。通常的做法是将stdin重定向到一个文件,然后再合并两个文件。而cat命令一次就能搞定这些操作。接下来你会看到该命令的基本用法和高级用法。

2.2.1 实战演练

cat命令是一个经常会用到的简单命令,它本身表示conCATenate(拼接)

用 cat 读取文件内容的一般语法

  1. $ cat file1 file2 file3 ...

该命令将作为命令行参数的文件内容拼接在一起并将结果发送到stdout

打印单个文件的内容

  1. $ cat file.txt
  2. This is a line inside file.txt
  3. This is the second line inside file.txt

打印多个文件的内容

  1. $ cat one.txt two.txt
  2. This line is from one.txt
  3. This line is from two.txt

cat命令不仅可以读取文件、拼接数据,还能够从标准输入中读取。

管道操作符可以将数据作为cat命令的标准输入:

  1. OUTPUT_FROM_SOME COMMANDS | cat

cat也可以将文件内容与终端输入拼接在一起。

下面的命令将stdin和另一个文件中的数据组合在一起:

  1. $ echo 'Text through stdin' | cat - file.txt

在上例中,-被作为stdin文本的文件名。

  1. [root@dev workspace]# cat file.txt
  2. This is a line inside file.txt
  3. This is the second line inside file.txt
  4. [root@dev workspace]# echo 'Text through stdin' | cat - file.txt
  5. Text through stdin
  6. This is a line inside file.txt
  7. This is the second line inside file.txt
  8. [root@dev workspace]#

2.2.2 补充内容

cat命令还有一些用于文件查看的选项。可以在终端会话中输入man cat来查看完整的选项
列表。

  1. $ yum install man #安装man pages
  2. $ yum -y install man-pages #安装pthread的man pages
  3. $ man cat

image.png

去掉多余的空白行

有时候文本文件中可能包含多处连续的空白行。如果你想删除这些额外的空白行,可以这样做:

  1. $ cat -s file

考虑下面的例子:

  1. $ cat multi_blanks.txt #压缩相邻的空白行
  2. line 1
  3. line2
  4. line3
  5. line4
  1. [root@dev workspace]# vi multi_blanks.txt
  2. [root@dev workspace]# cat multi_blanks.txt
  3. line 1
  4. line2
  5. line3
  6. line4
  7. [root@dev workspace]# cat -s multi_blanks.txt
  8. line 1
  9. line2
  10. line3
  11. line4
  12. [root@dev workspace]#

另外也可以用tr删除所有的空白行,我们会在2.6节详细讨论。

将制表符显示为^I

单从视觉上很难将制表符同连续的空格区分开。对于Python而言,制表符和空格是区别对待的。在文本编辑器中,两者看起来差不多,但是解释器将其视为不同的缩进。仅仅在文本编辑器中进行观察是很难发现这种错误的。cat有一个特性,可以将制表符识别出来。这有助于排查缩进错误。

cat命令的-T选项能够将制表符标记成^I。例如:

  1. $ cat file.py
  2. def function():
  3. var = 5
  4. next = 6
  5. third = 7
  6. $ cat -T file.py
  7. def function():
  8. ^Ivar = 5
  9. ^I^Inext = 6
  10. ^Ithird = 7^I
  1. [root@dev workspace]# vi file.py
  2. [root@dev workspace]# cat file.py
  3. def function():
  4. var = 5
  5. next = 6
  6. third = 7
  7. [root@dev workspace]# cat -T file.py
  8. def function():
  9. var = 5
  10. next = 6
  11. third = 7
  12. [root@dev workspace]#

行号

cat命令的-n选项会在输出的每一行内容之前加上行号。例如:

  1. $ cat lines.txt
  2. line
  3. line
  4. line
  1. [root@dev workspace]# vi lines.txt
  2. [root@dev workspace]# cat lines.txt
  3. line
  4. line
  5. line
  6. [root@dev workspace]# cat -n lines.txt
  7. 1 line
  8. 2 line
  9. 3 line
  10. [root@dev workspace]#

别担心,cat命令绝不会修改你的文件,它只是根据用户提供的选项在stdout中生成一个修改过的输出而已。可别尝试用重定向来覆盖输入文件。shell在打开输入文件之前会先创建新的输出文件。cat命令不允许使用相同的文件作为输入和重定向后的输出。利用管道并重定向输出会清空输入文件。

  1. $ echo "This will vanish" > myfile
  2. $ cat -n myfile >myfile
  3. cat: myfile: input file is output file
  4. $ cat myfile | cat -n >myfile
  5. $ ls -l myfile
  1. [root@dev workspace]# echo "This will vanish" > myfile
  2. [root@dev workspace]# ls
  3. debug.sh file.py file.txt lines.txt multi_blanks.txt myfile script.sh
  4. [root@dev workspace]# cat myfile
  5. This will vanish
  6. [root@dev workspace]# cat -n myfile
  7. 1 This will vanish
  8. [root@dev workspace]# cat -n myfile>myfile
  9. [root@dev workspace]# cat myfile
  10. [root@dev workspace]# cat -n myfile >myfile
  11. [root@dev workspace]# cat myfile
  12. [root@dev workspace]# cat myfile | cat -n >myfile
  13. [root@dev workspace]# ls -l myfile
  14. -rw-r--r-- 1 root root 0 Jan 19 09:52 myfile # myfile为空文件
  15. [root@dev workspace]#

选项-n会为包括空行在内的所有行生成行号。如果你想跳过空白行,可以使用选项-b