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。