Menu Close

在 Linux 中向文件追加内容

一) 概述

在本教程中,我们将探讨几种在 Linux 中向文件追加一行或多行内容的命令。

首先,我们将了解一些常见的 Linux 基本工具,如 echoprintfcat。然后,我们将探索一些不太为人所知的 Bash 工具,如 teesed

我们将在本指南中使用的所有命令都已在 Debian 12 (Bookworm) 上使用 GNU Bash 5.2.15 进行测试,但它们也应该适用于大多数其他 Linux 发行版。

二) 使用 Linux 基本工具

2.1. echo

echo 命令是 Linux Bash 中最常用和广泛使用的内置命令之一。通常,我们可以使用它将字符串显示到标准输出,默认情况下是终端:

echo "This line will be displayed to the terminal"

现在,我们将更改默认的标准输出,并将输入字符串重定向到文件中。这种功能由重定向操作符(>)提供。如果下面指定的文件已经包含一些数据,这些数据将会丢失:

echo "This line will be written into the file" > file.txt

为了向我们的 file.txt 文件追加一行而不覆盖其内容,我们需要使用另一个重定向操作符(>>):

echo "This line will be appended to the file" >> file.txt
echo "This line will be appended to the file" >> file.txt

请注意,>>> 操作符并不依赖于 echo 命令,它们可以重定向任何命令的输出:

ls -al >> result.txt
find . -type f >> result.txt

此外,我们可以使用 -e 选项启用对反斜杠转义字符的解释。因此,一些特殊字符,如换行符 \n,将被识别,我们可以向文件中追加多行内容:

echo -e "line3\n line4\n line5\n" >> file.txt

这将向 file.txt 文件追加三行新内容。

2.2. printf

printf 命令类似于同名的 C 函数。printf() 函数介绍

它以指定格式将任何参数打印到标准输出:

printf FORMAT [ARGUMENTS]

让我们构建一个示例,并使用重定向操作符向文件中追加一行内容:

printf "line%s!" "6" >> file.txt
与 echo 命令不同,当我们需要追加多行内容时,printf 的语法更简单。在这里,我们不需要指定特殊选项来使用换行符:
printf "line7\nline8!" >> file.txt

这将向 file.txt 的末尾添加两行新内容。

2.3. cat

cat 命令将文件或标准输入连接到标准输出。

它的语法与 echo 命令类似:

cat [OPTION] [FILE(s)]

不同之处在于,cat 接受一个或多个文件作为参数,而不是字符串,并按照指定的顺序将其内容复制到标准输出。

假设我们已经在一个或多个文件中有一些内容,并且我们想将它们追加到 result.txt 文件中:

cat file1.txt >> result.txt
cat file1.txt file2.txt file3.txt >> result.txt

第一个命令将 file1.txt 的内容追加到 result.txt,而第二个命令将 file1.txt、file2.txt 和 file3.txt 的内容追加到 result.txt。

接下来,让我们通过移除命令中的输入文件来学习另一种方法:

cat >> file.txt

在这种情况下,cat 命令将从终端读取数据,并将其追加到 file.txt

因此,我们可以在终端中输入一些内容(包括换行符),然后按 CTRL + D 退出:

cat >> file.txt
line1 using cat command
line2 using cat command
<press CTRL+D to exit>

这将向 file.txt 的末尾添加两行新内容。

这里有另一种方法,让 cat 命令读取输入,直到检测到特定的文本:

cat << EOF >> file.txt
line3 using cat command with EOF
line4 using cat command with EOF
EOF

在这个例子中,<< EOF 意味着我们希望 cat 从标准输入读取,直到遇到仅包含 EOF(文件结束)文本的行。同样,cat 的输出会像前面例子中一样写入 file.txt,即通过使用 >> 重定向操作符。

三). 使用 tee 命令

另一个有趣且实用的 Bash 命令是 tee 命令。它从标准输入读取数据,并将数据同时写入标准输出和文件:

tee [OPTION] [FILE(s)]
tee file1.txt
tee file1.txt file2.txt

为了将输入追加到文件中而不是覆盖其内容,我们需要使用 -a 选项:

tee -a file.txt
line1 using tee command

一旦我们按下 Enter 键,我们会看到相同的行被重复输出给我们:

tee -a file.txt
line1 using tee command
line1 using tee command
line2 using tee command
line2 using tee command
<press CTRL+D to exit>

现在,假设我们不想将输入追加到终端,只希望追加到文件中。这在 tee 命令中也是可能的。因此,我们可以省略文件参数,并使用重定向操作符将标准输入重定向到 file.txt

tee >> file.txt
line3 using tee command
<press CTRL+D to exit>

此外,我们可以以类似于之前使用 cat 的方式使用 tee,即让 tee 命令读取输入,直到检测到特定文本:

tee -a file.txt << EOF
line4 using tee command with EOF
line5 using tee command with EOF
EOF

最后,让我们查看 file.txt 的内容:

cat file.txt
This line will be written into the file
This line will be appended to the file
line3
line4
line5
line6
line7
line8
line1 using cat command
line2 using cat command
line3 using cat command with EOF
line4 using cat command with EOF
line1 using tee command
line2 using tee command
line3 using tee command
line4 using tee command with EOF
line5 using tee command with EOF

从上面的输出可以看出,我们已经成功使用多种 Bash 工具将多行内容追加到 file.txt 文件中。

四) 使用 sed

sed 命令(“stream editor”的缩写)是一个文本处理工具,用于对文本文件或流执行基本的文本转换:

sed [OPTION] [SCRIPT] [FILE...]

它通常用于执行查找替换操作、替换、删除、追加等多种操作。在本文中,我们将使用它来向文件追加行。

首先,让我们使用 echo 创建一个示例文件:

echo "This line will be written into the file" > file.txt

接下来,让我们向文件添加一行:

sed -i '$ a\This is a new line' file.txt

此命令使用 -i 选项直接修改文件,而不是将更改写入标准输出,从而就地向 file.txt 追加一行。它定位到文件的最后一行 ($),并使用 a\ 选项表示追加命令。然后,’This is a new line’ 这一行将被追加到文件中。

现在,让我们看看如果不添加 -i 选项会发生什么:

sed '$ a\This is line 3' file.txt
This line will be written into the file
This is a new line
This is line 3
cat file.txt
This line will be written into the file
This is a new line

这次更改没有写入文件,而是仅显示在标准输出上。

接下来,让我们向文件添加多行内容:

sed -i -e '$a\This is line 3' -e '$a\This is line 4' file.txt

我们使用 -e 选项来表示我们将为 sed 使用多个表达式或命令。

最后,让我们查看 file.txt 的内容:

cat file.txt
This line will be written into the file
This is a new line
This is line 3
This is line 4

至此,我们已经成功使用 echo 创建了一个新的文件 file.txt,然后使用 sed 向文件追加了三行新内容。

五)结论

在本教程中,我们探讨了几种可以帮助我们向文件追加一行或多行内容的方法。

首先,我们学习了 echoprintfcat 这几个 Bash 命令,并了解了如何将它们与重定向操作符结合使用,以便将文本追加到文件中。我们还学习了如何将一个或多个文件的内容追加到另一个文件中。

最后,我们探索了较少人知道的 teesed 命令,它们本身就具有将文本追加到文件的机制,不一定需要使用重定向操作符。

除教程外,本网站大部分文章来自互联网,如果有内容冒犯到你,请联系我们删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Leave the field below empty!

Posted in 文件

Related Posts