如何使用 cat命令示例

    1. 读取文件
      在最简单的形式中,cat 命令针对一个特定的文件运行,该文件将把文件的内容输出到屏幕上。在这里,我们阅读了我为这些示例创建的 test.txt 文件的内容。
[jack@onitroad ~]# cat test.txt
This is
a text  file

It      contains        text
        probably
    1. 读取所有文件
      我们还可以使用通配符来查看目录中的所有文件。
[jack@onitroad ~]# cat *

我们可以修改它以仅列出扩展名为 .txt 的文件的内容。

[jack@onitroad ~]# cat *.txt
    1. 数据行编号
      我们可以使用 -n 选项打印出每行的编号。
[jack@onitroad ~]# cat -n test.txt
     1  This is
     2  a text  file
     3
     4
     5
     6  It      contains        text
     7
     8          probably
    1. 对非空行编号
      或者,我们可以使用 -b 选项覆盖 -n 并且只会对非空行进行编号,因此不会对没有内容的行进行编号。
[jack@onitroad ~]# cat -b test.txt
     1  This is
     2  a text  file

     3  It      contains        text
     4          probably
    1. 显示标签字符
      我们可以使用 -T 选项将制表符显示为 '^I'。
[jack@onitroad ~]# cat -T test.txt
This is
a text^Ifile

It^Icontains^Itext
^Iprobably
    1. 显示行尾
      我们可以用 -E 显示行结束的位置,它会在每行的末尾添加一个“$”。
[jack@onitroad ~]# cat -E test.txt
This is$
a text  file$
$
$
$
It      contains        text$
$
        probably$
    1. 显示全部
      我们还可以使用 -A 显示所有内容,这类似于同时使用 -E 和 -T 来显示行尾和制表符。
[jack@onitroad ~]# cat -A test.txt
This is$
a text^Ifile$
$
$
$
It^Icontains^Itext$
$
^Iprobably$
    1. 隐藏重复的空行
      我们可以使用 -s 选项删除多个空行,该选项将压缩空行。在我们的示例中,我们有一个文件部分,其中一行有 3 个空行,使用 -s 后,这将更改为一个空行。
[jack@onitroad ~]# cat -s test.txt
This is
a text  file
It      contains        text
        probably
    1. 读取多个文件
      我们可以在第一个文件之后用 cat 指定更多文件,它会简单地读取所有文件,将输出连接在一起。
[jack@onitroad ~]# cat test.txt test2.txt
This is
a text  file

It      contains        text
        probably
This is test2.txt
    1. 将文件连接在一起
      与上面的示例类似,我们可以通过将一个文件添加到另一个文件来将多个文件连接在一起。请注意,这必须使用“>>”来完成,如果我们使用单个“>”,我们正在写入的文件将被覆盖而不是添加到。
[jack@onitroad ~]# cat test2.txt >> test.txt
[jack@onitroad ~]# cat test.txt
This is
a text  file

It      contains        text
        probably
This is test2.txt
    1. 将文件连接成新文件
      我们也可以将两个文件组合在一起形成一个新文件。
[jack@onitroad ~]# cat test.txt test2.txt > test-combination.txt
[jack@onitroad ~]# cat test-combination.txt
This is
a text  file

It      contains        text
        probably
This is test2.txt
This is test2.txt
    1. 反向 Cat 输出
      默认情况下,cat 命令从头到尾读取指定的文件,没有内置的方法来反转输出。然而,'tac' 命令(cat 向后)的工作方式与 cat 类似,只是文件中的行以相反的顺序打印出来。
[jack@onitroad ~]# tac test.txt
This is test2.txt
        probably
It      contains        text

a text  file
This is
    1. 读取标准输入
      我们可以从标准输入读取文件,而不是读取文件,即命令行。
[jack@onitroad ~]# cat
hello world!
hello world!

在这个例子中,输入 cat 后跟文本“hello world!”,文本从 cat 返回,这就是它出现两次的原因。

    1. 创建一个新文件
      我们还可以使用 cat 命令通过使用“>”后跟新文件名将输出重定向到文件来创建新文件。在这个例子中,我们运行 cat 从标准输入中读取并将所有输出发送到 text3.txt 文件。一旦我们输入了我们的文本,按“Ctrl+D”退出,然后我们将能够对新文件进行分类并确认它包含我们添加的内容。
[jack@onitroad ~]# cat > test3.txt
This is the test3 file from standard input!
[jack@onitroad ~]# cat test3.txt
This is the test3 file from standard input!
  • 15.打印cat 版本
    我们可以通过 --version 选项查看 cat 命令的版本信息。
[jack@onitroad ~]# cat --version
cat (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Torbjörn Granlund and jack M. Stallman.
    1. 打印帮助文档
      如果我们对 cat 命令有任何进一步的疑问,我们可以随时参考带有 --help 选项的帮助页面。请注意,为简洁起见,此命令的完整输出已被删除。
[jack@onitroad ~]# cat --help
Usage: cat [OPTION]... [FILE]...
Linux cat 命令示例

'cat' 命令用于读取和连接文件,将它们打印到标准输出。

Cat 来自 GNU Coreutils 包

日期:2020-07-07 20:57:02 来源:oir作者:oir