Linux運維基礎之sed命令詳解

Linux運維基礎之sed命令詳解

sed 是 “Stream Editor”(流編輯器)的縮寫,它是一款功能強大的實用工具,可讓你在命令列中直接解析和轉換文字。無論你處理的是配置檔案、指令碼還是純文字,sed 都是你快速高效處理文字的得力工具。

sed 的主要用途是搜尋特定的文字模式,並將其替換為其他內容。它還可以刪除或插入行,並執行其他文字轉換。它對於批量編輯檔案或在 shell 指令碼中自動執行各種任務特別有用。

雖然 sed 本身的功能非常多,但它經常與其他 Linux 命令結合使用,如用於文字處理的 awk、用於規則搜尋的 grep 和用於顯示檔案內容的 cat。這些工具共同構成了 Linux 環境下強大的文字處理工具包。

sed 命令的一般語法

$ sed [OPTIONS] [FILE]...

1. 文字替換

echo "Text" | sed 's/Replaceable_Word/The_Word_That_Replaces/'

使用 sed 命令搜尋並替換文字的任何部分。's' 表示搜尋和替換任務。
示例:

比方說,您有一個字串 ” I love CSS “,您想用 “CSS Libraries” 替換 “CSS”。

echo "I love CSS" | sed 's/CSS/CSS Libraries/'
I love CSS Libraries

在本例中,echo 命令輸出 ” I love CSS “,然後 sed 將 ” CSS” 替換為 ” CSS Libraries “。最後的輸出結果就是 “I love CSS Libraries“。

2. 替換檔案中特定行的文字

sed '[line] s/harder/easier/g' [file]

sed 命令的 'g' 選項用於替換任何與規則匹配的內容。

示例:

假設您有一個名為 example.txt 的文字檔案,內容如下:

Life is hard.
Working harder is the key to success.
The harder you work, the luckier you get.

如果要將 example.txt 第 2 行中所有出現的 ” harder” 一詞替換為 “easier“,您可以執行:

sed '2 s/harder/easier/g' example.txt

執行該命令後,終端上顯示的輸出將是:

Life is hard.
Working easier is the key to success.
The harder you work, the luckier you get.

請注意,”harder” 一詞只在第 2 行被替換為 “easier“。

如果想將這些更改儲存到檔案中,可以使用 -i 選項:

sed -i '2 s/harder/easier/g' example.txt

執行此命令後, example.txt 的內容將永久更改為:

Life is hard.
Working easier is the key to success.
The harder you work, the luckier you get.

3. 用新文字替換第一個匹配項

sed 's/harder/easier/' [file]

該命令只替換搜尋規則的第一個匹配項。

示例:

假設您有一個名為 example.txt 的文字檔案,內容如下:

Life is harder than we think.
Working harder is the key to success.
No pain, no gain. Work harder.

您可以使用 sed 命令將每一行中的 ” harder “替換為 “easier“:

sed 's/harder/easier/' example.txt

執行該命令後,輸出結果將是:

Life is easier than we think.
Working easier is the key to success.
No pain, no gain. Work easier.

4. 刪除匹配行

sed '/Something/d' example.txt

使用 sed 命令的 d 選項可以刪除檔案中的任意一行。

示例:

假設您有一個名為 example.txt 的檔案,內容如下:

Hello World
Something is here
Another line
Yet another line
Something else

執行命令 sed '/Something/d' example.txt 將輸出結果:

Hello World
Another line
Yet another line

5. 搜尋不區分大小寫的單詞 + 刪除

sed '/Sample/Id' example.txt

sed 命令的 I 選項用於以不區分大小寫的方式搜尋匹配模式。

示例:

假設您有一個名為 example.txt 的檔案,內容如下:

This is a Sample line.
Another line.
Yet another Sample line.
Final line.

執行 sed '/Sample/Id' example.txt 命令將產生以下輸出:

Another line.
Final line.

6. 用大寫字母替換單詞

sed 's/\(libraries\)/\U\1/Ig' example.txt

使用 sed 命令的 U 選項可將任何文字轉換為大寫字母。

示例:

假設您有一個名為 example.txt 的檔案,內容如下:

I love libraries.
libraries are great.
You can find many books in libraries.

執行 sed 命令後,輸出結果將是

I love LIBRARIES.
LIBRARIES are great.
You can find many books in LIBRARIES.

7. 用小寫字母替換單詞

sed 's/\(libraries\)/\L\1/Ig' example.txt

sed 命令的 L 選項用於將任何文字轉換為小寫字母。

示例:

假設您有一個名為 example.txt 的檔案,內容如下:

Libraries are essential for research.
libraries help in many ways.
I love LIBRARIES!

執行 sed 命令後,輸出結果將是:

libraries are essential for research.
libraries help in many ways.
I love libraries!

8. 在檔案中插入空行

sed G [file]

使用 sed 命令的 G 選項在檔案的每一行後插入空行。

示例:

假設您有一個名為 example.txt 的檔案,內容如下:

Hello
World
This
Is
A
Test

您可以執行以下命令,在每行末尾新增一個額外的換行符:

sed G example.txt

執行該命令後,輸出結果將是:

Hello

World

This

Is

A

Test

9. 列印檔案行號

sed '=' [file]

= 符號用於在檔案中每行文字前列印行號。

示例:

假設您有一個名為 example.txt 的檔案,內容如下:

Hello
World
This
Is
A
Test

您可以執行以下命令來列印每行前的行號:

sed '=' example.txt
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。