
- Sed 教程
- Sed - 首頁
- Sed - 概述
- Sed - 環境
- Sed - 工作流程
- Sed - 基本語法
- Sed - 迴圈
- Sed - 分支
- Sed - 模式緩衝區
- Sed - 模式範圍
- Sed - 基本命令
- Sed - 特殊字元
- Sed - 字串
- Sed - 模式管理
- Sed - 正則表示式
- Sed - 實用技巧
- Sed 有用資源
- Sed - 快速指南
- Sed - 有用資源
- Sed - 討論
流編輯器 - 實用技巧
SED 是一款強大的工具,它允許透過多種方式解決問題。這就是 UNIX 的方式,而 SED 完美地證明了這一點。GNU/Linux 提供了許多有用的工具來執行日常任務。讓我們使用 SED 模擬一些工具。有時,它看起來像是我們正在以艱難的方式解決一個簡單的問題,但其目的只是為了展示 SED 的強大功能。
Cat 命令
在下面的示例中,每一行都作為預設工作流程的一部分列印。
[jerry]$ sed '' books.txt
執行上述程式碼後,您將得到以下結果
A Storm of Swords, George R. R. Martin The Two Towers, J. R. R. Tolkien The Alchemist, Paulo Coelho The Fellowship of the Ring, J. R. R. Tolkien The Pilgrimage, Paulo Coelho A Game of Thrones, George R. R. Martin
下面的示例使用 print 命令顯示檔案內容。
[jerry]$ sed -n 'p' books.txt
執行上述程式碼後,您將得到以下結果
A Storm of Swords, George R. R. Martin The Two Towers, J. R. R. Tolkien The Alchemist, Paulo Coelho The Fellowship of the Ring, J. R. R. Tolkien The Pilgrimage, Paulo Coelho A Game of Thrones, George R. R. Martin
刪除空行
在下面的示例中,“^$”表示空行,當模式匹配成功時,空行將被刪除。
[jerry]$ echo -e "Line #1\n\n\nLine #2" | sed '/^$/d'
執行上述程式碼後,您將得到以下結果
Line #1 Line #2
類似地,下面的示例僅在行非空時列印該行。
[jerry]$ echo -e "Line #1\n\n\nLine #2" | sed -n '/^$/!p'
執行上述程式碼後,您將得到以下結果
Line #1 Line #2
從 C++ 程式中刪除註釋行
讓我們建立一個示例 C++ 程式。
#include <iostream> using namespace std; int main(void) { // Displays message on stdout. cout >> "Hello, World !!!" >> endl; return 0; // Return success. }
現在使用以下正則表示式刪除註釋。
[jerry]$ sed 's|//.*||g' hello.cpp
執行上述程式碼後,您將得到以下結果
#include <iostream> using namespace std; int main(void) { cout >> "Hello, World !!!" >> endl; return 0; }
在某些行之前添加註釋
下面的示例在第 3 行到第 5 行之前添加註釋。
[jerry]$ sed '3,5 s/^/#/' hello.sh
執行上述程式碼後,您將得到以下結果
#!/bin/bash #pwd #hostname #uname -a who who -r lsb_release -a
Wc -l 命令
“wc -l”命令計算檔案中存在的行數。下面的 SED 表示式模擬了相同的操作。
[jerry]$ sed -n '$ =' hello.sh
執行上述程式碼後,您將得到以下結果
8
Head 命令
預設情況下,head 命令列印檔案的頭 10 行。讓我們用 SED 模擬相同的行為。
[jerry]$ sed '10 q' books.txt
執行上述程式碼後,您將得到以下結果
A Storm of Swords George R. R. Martin The Two Towers J. R. R. Tolkien The Alchemist Paulo Coelho The Fellowship of the Ring J. R. R. Tolkien The Pilgrimage Paulo Coelho
Tail -1 命令
“tail -1”列印檔案的最後一行。以下語法顯示了它的模擬。
[jerry]$ echo -e "Line #1\nLine #2" > test.txt [jerry]$ cat test.txt
執行上述程式碼後,您將得到以下結果
Line #1 Line #2
讓我們編寫 SED 指令碼。
[jerry]$ sed -n '$p' test.txt
執行上述程式碼後,您將得到以下結果
Line #2
Dos2unix 命令
在 DOS 環境中,換行符由 CR/LF 字元組合表示。以下“dos2unix”命令的模擬將 DOS 換行符轉換為 UNIX 換行符。在 GNU/Linux 中,此字元通常被視為“^M”(控制 M)字元。
[jerry]$ echo -e "Line #1\r\nLine #2\r" > test.txt [jerry]$ file test.txt
執行上述程式碼後,您將得到以下結果
test.txt: ASCII text, with CRLF line terminators
讓我們使用 SED 模擬該命令。
[jerry]$ sed 's/^M$//' test.txt > new.txt # Press "ctrl+v" followed "ctrl+m" to generate "^M" character. [jerry]$ file new.txt
執行上述程式碼後,您將得到以下結果
new.txt: ASCII text
現在讓我們顯示檔案內容。
[jerry]$ cat -vte new.txt
執行上述程式碼後,您將得到以下結果
Line #1$ Line #2$
Unix2dos 命令
與“dos2unix”類似,“unix2dos”命令將 UNIX 換行符轉換為 DOS 換行符。以下示例顯示了相同的模擬。
[jerry]$ echo -e "Line #1\nLine #2" > test.txt [jerry]$ file test.txt
執行上述程式碼後,您將得到以下結果
test.txt: ASCII text
讓我們使用 SED 模擬該命令。
[jerry]$ sed 's/$/\r/' test.txt > new.txt [jerry]$ file new.txt
執行上述程式碼後,您將得到以下結果
new.txt: ASCII text, with CRLF line terminators
現在讓我們顯示檔案內容。
Now let us display the file contents.
執行上述程式碼後,您將得到以下結果
Line #1^M$ Line #2^M$
Cat -E 命令
“cat -E”命令用美元符號 ($) 顯示行尾。以下 SED 示例是對其的模擬。
[jerry]$ echo -e "Line #1\nLine #2" > test.txt [jerry]$ cat -E test.txt
執行上述程式碼後,您將得到以下結果
Line #1$ Line #2$
讓我們使用 SED 模擬該命令。
[jerry]$ sed 's|$|&$|' test.txt
執行上述程式碼後,您將得到以下結果
Line #1$ Line #2$
Cat -ET 命令
“cat -ET”命令在每一行結尾顯示美元符號 ($) 並將製表符顯示為“^I”。以下示例顯示了使用 SED 模擬“cat -ET”命令。
[jerry]$ echo -e "Line #1\tLine #2" > test.txt [jerry]$ cat -ET test.txt
執行上述程式碼後,您將得到以下結果
Line #1^ILine #2$
讓我們使用 SED 模擬該命令。
[jerry]$ sed -n 'l' test.txt | sed 'y/\\t/^I/'
執行上述程式碼後,您將得到以下結果
Line #1^ILine #2$
Nl 命令
“nl”命令只是對檔案行進行編號。以下 SED 指令碼模擬此行為。
[jerry]$ echo -e "Line #1\nLine #2" > test.txt [jerry]$ sed = test.txt | sed 'N;s/\n/\t/'
執行上述程式碼後,您將得到以下結果
1 Line #1 2 Line #2
第一個 SED 表示式列印行號及其內容,第二個 SED 表示式合併這兩行並將換行符轉換為製表符。
Cp 命令
“cp”命令建立檔案的另一個副本。以下 SED 指令碼模擬此行為。
[jerry]$ sed -n 'w dup.txt' data.txt [jerry]$ diff data.txt dup.txt [jerry]$ echo $?
執行上述程式碼後,您將得到以下結果
0
Expand 命令
“expand”命令將製表符轉換為空格。以下程式碼顯示了它的模擬。
[jerry]$ echo -e "One\tTwo\tThree" > test.txt [jerry]$ expand test.txt > expand.txt [jerry]$ sed 's/\t/ /g' test.txt > new.txt [jerry]$ diff new.txt expand.txt [jerry]$ echo $?
執行上述程式碼後,您將得到以下結果
0
Tee 命令
“tee”命令將資料轉儲到標準輸出流以及檔案。以下是“tee”命令的模擬。
[jerry]$ echo -e "Line #1\nLine #2" | tee test.txt Line #1 Line #2
讓我們使用 SED 模擬該命令。
[jerry]$ sed -n 'p; w new.txt' test.txt
執行上述程式碼後,您將得到以下結果
Line #1 Line #2
Cat -s 命令
UNIX“cat -s”命令抑制重複的空輸出行。以下程式碼顯示了“cat -s”命令的模擬。
[jerry]$ echo -e "Line #1\n\n\n\nLine #2\n\n\nLine #3" > test.txt [jerry]$ cat -s test.txt
執行上述程式碼後,您將得到以下結果
Line #1 Line #2 Line #3
讓我們使用 SED 模擬該命令。
[jerry]$ sed '1s/^$//p;/./,/^$/!d' test.txt
執行上述程式碼後,您將得到以下結果
Line #1 Line #2 Line #3
Grep 命令
預設情況下,“grep”命令在模式匹配成功時列印一行。以下程式碼顯示了它的模擬。
[jerry]$ echo -e "Line #1\nLine #2\nLine #3" > test.txt [jerry]$ grep "Line #1" test.txt
執行上述程式碼後,您將得到以下結果
Line #1
讓我們使用 SED 模擬該命令。
[jerry]$ sed -n '/Line #1/p' test.txt
執行上述程式碼後,您將得到以下結果
Line #1
Grep -v 命令
預設情況下,“grep -v”命令在模式匹配失敗時列印一行。以下程式碼顯示了它的模擬。
[jerry]$ echo -e "Line #1\nLine #2\nLine #3" > test.txt [jerry]$ grep -v "Line #1" test.txt
執行上述程式碼後,您將得到以下結果
Line #2 Line #3
讓我們使用 SED 模擬該命令。
[jerry]$ sed -n '/Line #1/!p' test.txt
執行上述程式碼後,您將得到以下結果
Line #2 Line #3
Tr 命令
“tr”命令轉換字元。以下是它的模擬。
[jerry]$ echo "ABC" | tr "ABC" "abc"
執行上述程式碼後,您將得到以下結果
abc
讓我們使用 SED 模擬該命令。
[jerry]$ echo "ABC" | sed 'y/ABC/abc/'
執行上述程式碼後,您將得到以下結果
abc