Sed實用功能

SED是一個了不起的工具,它允許多種方式來解決問題。 GNU/ Linux提供了許多有用的實用程序來執行日常的日常任務。讓我們模擬使用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

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

下面的示例使用打印命令來顯示文件內容。

[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 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 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 命令

Unix的"wc -l" 命令的計數存在於文件中的行數。下面的sed表達式模擬相同。

[jerry]$ sed -n '$ =' hello.sh

執行上面的代碼,會得到如下結果:

8

head 命令

默認情況下,Unix的head命令打印前3行的文件。讓我們模擬Sed相同的行爲。

[jerry]$ sed '3 q' books.txt

執行上面的代碼,會得到如下結果:

A Storm of Swords, George R. R. Martin
The Two Towers, J. R. R. Tolkien
The Alchemist, Paulo Coelho

tail -1 命令

Unix的「tail -1」打印文件的最後一行。下面的語法顯示了模擬。

[jerry]$ sed -n '$p' books.txt

執行上面的代碼,會得到如下結果:

A Game of Thrones, George R. R. Martin

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模擬命令。

# Press "ctrl+v" followed "ctrl+m" to generate [jerry]$ sed 's/^M$//' test.txt > new.txt "^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」命令顯示每行的末尾美元($)符號,並顯示TAB字符「^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 表達式融合這兩行,並轉換換行符到TAB字符。

cp 命令

"cp"命令創建文件的另一個副本。以下sed腳本來模擬這種行爲。

[jerry]$ sed -n 'w dup.txt' data.txt [jerry]$ diff data.txt dup.txt [jerry]$ echo $?

執行上面的代碼,會得到如下結果:

0

expand 命令

「expand」命令TAB字符轉換爲空格。下面的代碼顯示了模擬。

[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 命令

UNIXcat -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