Linux运维基础之sed命令详解

Linux运维基础之sed命令详解

sed 是 “Stream Editor”(流编辑器)的缩写,它是一款功能强大的实用工具,可让你在命令行中直接解析和转换文本。无论你处理的是配置文件、脚本还是纯文本,sed 都是你快速高效处理文本的得力工具。

sed 的主要用途是搜索特定的文本模式,并将其替换为其他内容。它还可以删除或插入行,并执行其他文本转换。它对于批量编辑文件或在 shell 脚本中自动执行各种任务特别有用。

虽然 sed 本身的功能非常多,但它经常与其他 Linux 命令结合使用,如用于文本处理的 awk、用于规则搜索的 grep 和用于显示文件内容的 cat。这些工具共同构成了 Linux 环境下强大的文本处理工具包。

sed 命令的一般语法

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ sed [OPTIONS] [FILE]...
$ sed [OPTIONS] [FILE]...
$ sed [OPTIONS] [FILE]...

1. 文本替换

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo "Text" | sed 's/Replaceable_Word/The_Word_That_Replaces/'
echo "Text" | sed 's/Replaceable_Word/The_Word_That_Replaces/'
echo "Text" | sed 's/Replaceable_Word/The_Word_That_Replaces/'

使用 sed 命令搜索并替换文本的任何部分。's' 表示搜索和替换任务。
示例:

比方说,您有一个字符串 ” I love CSS “,您想用 “CSS Libraries” 替换 “CSS”。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo "I love CSS" | sed 's/CSS/CSS Libraries/'
echo "I love CSS" | sed 's/CSS/CSS Libraries/'
echo "I love CSS" | sed 's/CSS/CSS Libraries/'
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
I love CSS Libraries
I love CSS Libraries
I love CSS Libraries

在本例中,echo 命令输出 ” I love CSS “,然后 sed 将 ” CSS” 替换为 ” CSS Libraries “。最后的输出结果就是 “I love CSS Libraries“。

2. 替换文件中特定行的文本

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sed '[line] s/harder/easier/g' [file]
sed '[line] s/harder/easier/g' [file]
sed '[line] s/harder/easier/g' [file]

sed 命令的 'g' 选项用于替换任何与规则匹配的内容。

示例:

假设您有一个名为 example.txt 的文本文件,内容如下:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Life is hard.
Working harder is the key to success.
The harder you work, the luckier you get.
Life is hard. Working harder is the key to success. The harder you work, the luckier you get.
Life is hard.
Working harder is the key to success.
The harder you work, the luckier you get.

如果要将 example.txt 第 2 行中所有出现的 ” harder” 一词替换为 “easier“,您可以运行:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sed '2 s/harder/easier/g' example.txt
sed '2 s/harder/easier/g' example.txt
sed '2 s/harder/easier/g' example.txt

运行该命令后,终端上显示的输出将是:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Life is hard.
Working easier is the key to success.
The harder you work, the luckier you get.
Life is hard. Working easier is the key to success. The harder you work, the luckier you get.
Life is hard.
Working easier is the key to success.
The harder you work, the luckier you get.

请注意,”harder” 一词只在第 2 行被替换为 “easier“。

如果想将这些更改保存到文件中,可以使用 -i 选项:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sed -i '2 s/harder/easier/g' example.txt
sed -i '2 s/harder/easier/g' example.txt
sed -i '2 s/harder/easier/g' example.txt

运行此命令后, example.txt 的内容将永久更改为:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Life is hard.
Working easier is the key to success.
The harder you work, the luckier you get.
Life is hard. Working easier is the key to success. The harder you work, the luckier you get.
Life is hard.
Working easier is the key to success.
The harder you work, the luckier you get.

3. 用新文本替换第一个匹配项

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sed 's/harder/easier/' [file]
sed 's/harder/easier/' [file]
sed 's/harder/easier/' [file]

该命令只替换搜索规则的第一个匹配项。

示例:

假设您有一个名为 example.txt 的文本文件,内容如下:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Life is harder than we think.
Working harder is the key to success.
No pain, no gain. Work harder.
Life is harder than we think. Working harder is the key to success. No pain, no gain. Work harder.
Life is harder than we think.
Working harder is the key to success.
No pain, no gain. Work harder.

您可以使用 sed 命令将每一行中的 ” harder “替换为 “easier“:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sed 's/harder/easier/' example.txt
sed 's/harder/easier/' example.txt
sed 's/harder/easier/' example.txt

运行该命令后,输出结果将是:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Life is easier than we think.
Working easier is the key to success.
No pain, no gain. Work easier.
Life is easier than we think. Working easier is the key to success. No pain, no gain. Work easier.
Life is easier than we think.
Working easier is the key to success.
No pain, no gain. Work easier.

4. 删除匹配行

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sed '/Something/d' example.txt
sed '/Something/d' example.txt
sed '/Something/d' example.txt

使用 sed 命令的 d 选项可以删除文件中的任意一行。

示例:

假设您有一个名为 example.txt 的文件,内容如下:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Hello World
Something is here
Another line
Yet another line
Something else
Hello World Something is here Another line Yet another line Something else
Hello World
Something is here
Another line
Yet another line
Something else

运行命令 sed '/Something/d' example.txt 将输出结果:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Hello World
Another line
Yet another line
Hello World Another line Yet another line
Hello World
Another line
Yet another line

5. 搜索不区分大小写的单词 + 删除

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sed '/Sample/Id' example.txt
sed '/Sample/Id' example.txt
sed '/Sample/Id' example.txt

sed 命令的 I 选项用于以不区分大小写的方式搜索匹配模式。

示例:

假设您有一个名为 example.txt 的文件,内容如下:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
This is a Sample line.
Another line.
Yet another Sample line.
Final line.
This is a Sample line. Another line. Yet another Sample line. Final line.
This is a Sample line.
Another line.
Yet another Sample line.
Final line.

运行 sed '/Sample/Id' example.txt 命令将产生以下输出:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Another line.
Final line.
Another line. Final line.
Another line.
Final line.

6. 用大写字母替换单词

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sed 's/\(libraries\)/\U\1/Ig' example.txt
sed 's/\(libraries\)/\U\1/Ig' example.txt
sed 's/\(libraries\)/\U\1/Ig' example.txt

使用 sed 命令的 U 选项可将任何文本转换为大写字母。

示例:

假设您有一个名为 example.txt 的文件,内容如下:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
I love libraries.
libraries are great.
You can find many books in libraries.
I love libraries. libraries are great. You can find many books in libraries.
I love libraries.
libraries are great.
You can find many books in libraries.

运行 sed 命令后,输出结果将是

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
I love LIBRARIES.
LIBRARIES are great.
You can find many books in LIBRARIES.
I love LIBRARIES. LIBRARIES are great. You can find many books in LIBRARIES.
I love LIBRARIES.
LIBRARIES are great.
You can find many books in LIBRARIES.

7. 用小写字母替换单词

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sed 's/\(libraries\)/\L\1/Ig' example.txt
sed 's/\(libraries\)/\L\1/Ig' example.txt
sed 's/\(libraries\)/\L\1/Ig' example.txt

sed 命令的 L 选项用于将任何文本转换为小写字母。

示例:

假设您有一个名为 example.txt 的文件,内容如下:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Libraries are essential for research.
libraries help in many ways.
I love LIBRARIES!
Libraries are essential for research. libraries help in many ways. I love LIBRARIES!
Libraries are essential for research.
libraries help in many ways.
I love LIBRARIES!

运行 sed 命令后,输出结果将是:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
libraries are essential for research.
libraries help in many ways.
I love libraries!
libraries are essential for research. libraries help in many ways. I love libraries!
libraries are essential for research.
libraries help in many ways.
I love libraries!

8. 在文件中插入空行

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sed G [file]
sed G [file]
sed G [file]

使用 sed 命令的 G 选项在文件的每一行后插入空行。

示例:

假设您有一个名为 example.txt 的文件,内容如下:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Hello
World
This
Is
A
Test
Hello World This Is A Test
Hello
World
This
Is
A
Test

您可以运行以下命令,在每行末尾添加一个额外的换行符:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sed G example.txt
sed G example.txt
sed G example.txt

运行该命令后,输出结果将是:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Hello
World
This
Is
A
Test
Hello World This Is A Test
Hello

World

This

Is

A

Test

9. 打印文件行号

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sed '=' [file]
sed '=' [file]
sed '=' [file]

= 符号用于在文件中每行文本前打印行号。

示例:

假设您有一个名为 example.txt 的文件,内容如下:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Hello
World
This
Is
A
Test
Hello World This Is A Test
Hello
World
This
Is
A
Test

您可以运行以下命令来打印每行前的行号:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
sed '=' example.txt
sed '=' example.txt
sed '=' example.txt
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
1
Hello
2
World
3
This
4
Is
5
A
6
Test
1 Hello 2 World 3 This 4 Is 5 A 6 Test
1
Hello
2
World
3
This
4
Is
5
A
6
Test

更多 Linux 命令

下面罗列了最常见的一些 Linux 命令,您可以根据自己的需要查阅对应命令的详细解析:

目录操作 rmdir · cd · pwd · exa · ls
文件操作 cat · cp · dd · less · touch · ln · rename · more · head
文件系统操作 chown · mkfs · locate
网络 ping · curl · wget · iptables · mtr
搜索和文本处理 find · grep · sed · whatis · ripgrep · fd · tldr
系统信息和管理 env · history · top · who · htop · glances · lsof
用户和会话管理 screen · su · sudo · open

此外,我们还整理 Linux 命令行大全,以帮助大家全面深入地学习 Linux。