在 Linux 中使用 cat 命令

系统 Linux
​​cat​​ 命令用于打印文本文件的文件内容。至少,大多数 Linux 用户都是这么做的,而且没有什么问题。

cat 命令的用途不仅仅是显示文件内容。

cat 命令用于打印文本文件的文件内容。至少,大多数 Linux 用户都是这么做的,而且没有什么问题。

cat 实际上代表 “连接concatenate”,创建它是为了 合并文本文件。但只要有一个参数,它就会打印文件内容。因此,它是用户在终端中读取文件而无需任何其他选项的首选。

在 Linux 中使用 cat 命令

要使用 cat 命令,你必须遵循给定的命令语法:

cat [options] Filename(s)

这里:

  • [options] 用于修改 cat 命令的默认行为,例如使用 -n 选项获取每行的数字。
  • Filename 是你输入要使用的文件的文件名的位置。

为了简单起见,我将在本指南中使用名为 Haruki.txt 的文本文件,其中包含以下文本行:

Hear the Wind Sing (1979)
Pinball, 1973 (1980)
A Wild Sheep Chase (1982)
Hard-Boiled Wonderland and the End of the World (1985)
Norwegian Wood (1987)
Dance Dance Dance (1990)
South of the Border, West of the Sun (1992)
The Wind-Up Bird Chronicle (1994)
Sputnik Sweetheart (1999)
Kafka on the Shore (2002)
After Dark (2004)
1Q84 (2009-2010)
Colorless Tsukuru Tazaki and His Years of Pilgrimage (2013)
Men Without Women (2014)
Killing Commendatore (2017)

那么,在没有任何选项的情况下使用时,输出会是什么? 好吧,让我们看一下:

cat Haruki.txt

use cat command in Linuxuse cat command in Linux

正如你所看到的,它打印了整个文本文件!

但你可以做的远不止这些。让我向你展示一些实际例子。

1、创建新文件

大多数 Linux 用户使用 touch 命令来 创建新文件,但使用 cat 命令也可以完成相同的操作!

在这种场景下,cat 命令比 touch 命令有一个优势,因为你可以在创建文件时向文件添加文本。听起来很酷。不是吗?

为此,你需要使用 cat 命令,将文件名附加到 > 后面,如下所示:

cat > Filename

例如,在这里,我创建了一个名为 NewFile.txt 的文件:

cat > NewFile.txt

当你这样做了,就会有一个闪烁的光标要求你写一些东西,最后,你可以使用 Ctrl + d 来保存更改。

如果你想创建一个空文件,则只需按 Ctrl + d 而不进行任何更改。

Using cat commandUsing cat command

这就好了!现在,你可以使用 ls 命令来显示 当前工作目录的内容

use the ls command to list the contents of the current working directoryuse the ls command to list the contents of the current working directory

2、将文件内容复制到另一个文件

考虑一个场景,你要将 FileA 的文件内容重定向到 FileB

当然,你可以复制和粘贴。但是如果有几百或几千行怎么办?

简单。你可以使用 cat 命令来重定向数据流。为此,你必须遵循给定的命令语法:

cat FileA > FileB

🚧 如果使用上述语法重定向文件内容,它将删除 FileB 的文件内容,然后重定向 FileA 的文件内容。

例如,我将使用两个文本文件 FileA 和 FileB,其中包含以下内容:

check the file contents using the cat commandcheck the file contents using the cat command

现在,如果我使用从 FileA 到 FileB 的重定向,它将删除 FileB 的数据,然后重定向 FileA 的数据:

cat FileA > FileB

redirect the file content using the cat commandredirect the file content using the cat command

同样,你可以对多个文件执行相同的操作:

cat FileA FileB > FileC

redirect file content of multiple files using the cat commandredirect file content of multiple files using the cat command

可以看到,上面的命令删除了 FileC 的数据,然后重定向了 FileA 和 FileB 的数据。

3、将一个文件的内容附加到另一个文件

有时你想要将数据附加到现有数据,在这种情况下,你必须使用 >> 而不是单个 >

cat FileA >> FileB

例如,在这里,我将把两个文件 FileA 和 FileB 重定向到 FileC

cat FileA.txt FileB.txt >> FileC.txt

redirect file content without overriding using the cat commandredirect file content without overriding using the cat command

如你所见,它保留了 FileC.txt 的数据,并将数据附加在末尾。

💡 你可以使用 >> 向现有文件添加新行。使用 cat >> filename 并开始添加所需的文本,最后使用 Ctrl+D 保存更改。

4、显示行数

你可能会遇到这样的情况,你想查看行数,这可以使用 -n 选项来实现:

cat -n File

例如,在这里,我将 -n 选项与 Haruki.txt 一起使用:

get the number of the lines in the cat commandget the number of the lines in the cat command

5、删除空行

在文本文档中留下多个空白行? cat 命令将为你修复它!

为此,你所要做的就是使用 -s 标志。

但使用 -s 标志有一个缺点。你仍然留有一行空白:

remove blank lines with the cat commandremove blank lines with the cat command

正如你所看到的,它有效,但结果接近预期。

那么如何删除所有空行呢? 通过管道将其传递给 grep 命令:

cat File | grep -v '^$'

这里,-v 标志将根据指定的模式过滤掉结果,'^$' 是匹配空行的正则表达式。

以下是我在 Haruki.txt 上使用它时的结果:

cat Haruki.txt | grep -v '^$'

remove all the blank lines in text files using the cat command piped with grep regular expressionremove all the blank lines in text files using the cat command piped with grep regular expression

当获得完美的输出,你可以将其重定向到文件以保存输出:

cat Haruki.txt | grep -v '^$' > File

save output of cat command by redirectionsave output of cat command by redirection

这就是你到目前为止所学到的

以下是我在本教程中解释的内容的快速摘要:

命令

描述

cat <Filename>

将文件内容打印到终端。

cat >File

创建一个新文件。

cat FileA > FileB

FileB 的文件内容将被 FileA 覆盖。

cat FileA >> FileB

FileA 的文件内容将附加到 FileB 的末尾。

cat -n File

显示行数,同时省略文件的文件内容。

cat File | more

将 cat 命令通过管道连接到 more 命令以处理大文件。请记住,它不能让你向上滚动!

cat File | less

将 cat 命令通过管道传输到 less 命令,这与上面类似,但它允许你双向滚动。

cat File | grep -v '^$'

从文件中删除所有空行。

🏋️ 练习时间

如果你学到了新东西,用不同的可能性来执行它是最好的记忆方式。

为此,你可以使用 cat 命令进行一些简单的练习。它们将是超级基本的,就像 cat 一样是最基本的命令之一

出于练习目的,你可以 使用 GitHub 上的文本文件

  • 如何使用 cat 命令创建空文件?
  • 将 cat 命令生成的输出重定向到新文件 IF.txt
  • 能否将三个或更多文件输入重定向到一个文件? 如果是,该如何做?
责任编辑:庞桂玉 来源: Linux中国
相关推荐

2023-07-04 16:36:03

Linuxcd 命令

2023-08-12 15:05:26

Linuxcp 命令

2020-12-07 06:25:14

Linux Truncate 命令

2010-06-24 11:16:17

Linux Cat命令详解

2018-08-21 09:00:30

Linuxtop命令

2023-01-13 23:21:29

netcat命令Linux

2022-08-10 13:12:04

Linuxcat命令

2022-10-18 10:00:09

Linuxtcpdump命令

2018-11-05 13:50:44

Linux命令tcpdump

2010-01-15 19:37:36

Linux命令

2010-01-05 16:49:34

2013-05-14 10:13:06

WindowsLinux操作系统

2010-06-24 14:08:25

Linux Cat命令

2021-04-16 11:18:56

LinuxlsusbUSB

2022-10-25 09:07:28

Linuxxargs命令

2021-01-04 05:43:59

LinuxBasename命令

2022-11-01 16:19:47

Powershell开源命令

2021-03-17 08:07:28

Linux Lsusb命令

2018-06-26 09:15:24

Linux命令history

2018-05-16 10:32:06

Linux命令find
点赞
收藏

51CTO技术栈公众号