linux命令總結sed命令詳解
Sed 簡介
sed 是一種新型的,非交互式的編輯器。它能執行與編輯器 vi 和 ex 相同的編輯任務。sed 編輯器沒有提供交互式使用方式,使用者只能在命令行輸入編輯命令、指定文件名,然后在屏幕上查看輸出。 sed 編輯器沒有破壞性,它不會修改文件,除非使用 shell 重定向來保存輸出結果。默認情況下,所有的輸出行都被打印到屏幕上。
sed 工作過程
sed 編輯器逐行處理文件(或輸入),並將輸出結果發送到屏幕。 sed 的命令就是在 vi和 ed/ex 編輯器中見到的那些。 sed 把當前正在處理的行保存在一個臨時緩存區中,這個緩存區稱為模式空間或臨時緩沖。sed 處理完模式空間中的行后(即在該行上執行 sed 命令后),就把改行發送到屏幕上(除非之前有命令刪除這一行或取消打印操作)。 sed 每處理完輸入文件的最后一行后, sed 便結束運行。 sed 把每一行都存在臨時緩存區中,對這個副本進行編輯,所以不會修改或破壞源文件。如圖 1: sed 處理過程。
從上圖可以看出 sed 不是破壞性的,它不會修改正在編輯的文件。
Sed 命令格式
1
|
sed
命令行格式為:
sed
[選項] ‘
command
’ 輸入文本
|
Sed 定位
Sed 命令在沒有給定的位置時,默認會處理所有行;
Sed 支持一下幾種地址類型:
1、 first~step
這兩個單詞的意思: first 指起始匹配行, step 指步長,例如: sed -n 2~5p 含義:從第二行開始匹配,隔 5 行匹配一次,即 2,7,12.......。
2、 $
這個$符表示匹配最后一行。
3、 /REGEXP/
這個是表示匹配正則那一行,通過//之間的正則來匹配。
4、 \cREGEXPc
這個是表示匹配正則那一行,通過\c 和 c 之間的正則來匹配,c 可以是任一字符
5、 addr1, add2
定址 addr1, add2 決定用於對哪些行進行編輯。地址的形式可以是數字、正則表達式或二者的結合。如果沒有指定地址, sed 將處理輸入文件中的所有行。如果定址是一個數字,則這個數字代表行號,如果是逗號分隔的兩個行號,那么需要處理的定址就是兩行之間的范圍(包括兩行在內)。范圍可以是數字,正則或二者組合。
6、 addr1, +N
從 addr1 這行到往下 N 行匹配,總共匹配 N+1 行
7、 addr1, ~N
Will match addr1 and the lines following addr1 until the next line whose input line number is a multiple of N.【沒有看懂是什么意思】
Sed 的正則表達式
表 1: sed 的正則表達式元字符
元字符 | 功 能 | 示 例 | 示例的匹配對象 |
^ | 行首定位符 | /^love/ | 匹配所有以 love 開頭的行 |
$ | 行尾定位符 | /love$/ | 匹配所有以 love 結尾的行 |
. | 匹配除換行外的單 個字符 |
/l..e/ | 匹配包含字符 l、后跟兩個任意 字符、再跟字母 e 的行 |
* | 匹配零個或多個前 導字符 |
/*love/ | 匹配在零個或多個空格緊跟着 模式 love 的行 |
[] | 匹配指定字符組內 任一字符 |
/[Ll]ove/ | 匹配包含 love 和 Love 的行 |
[^] | 匹配不在指定字符 組內任一字符 |
/[^A-KM-Z]ove/ | 匹配包含 ove,但 ove 之前的那 個字符不在 A 至 K 或 M 至 Z 間 的行 |
.. |
保存已匹配的字符 | |||
& | 保存查找串以便在 替換串中引用 |
s/love/**&**/ | 符號&代表查找串。字符串 love 將替換前后各加了兩個**的引 用,即 love 變成**love** |
\< | 詞首定位符 | /\<love/ | 匹配包含以 love 開頭的單詞的 行 |
\> | 詞尾定位符 | /love\>/ | 匹配包含以 love 結尾的單詞的 行 |
x\{m\} | 連續 m 個 x | /o\{5\}/ | 分別匹配出現連續 5 個字母 o、 至少 5 個連續的 o、或 5~10 個 連續的 o 的行 |
x\{m,\} | 至少 m 個 x | /o\{5,\}/ | |
x\{m,n\} | 至少 m 個 x,但不 超過 n 個 x |
/o\{5,10\}/ |
sed的常用選項
表 2.sed 的常用選項
選項 | 說明 |
-n | 使用安靜模式,在一般情況所有的 STDIN 都會輸出到屏幕上,加入-n 后只打印 被 sed 特殊處理的行 |
-e | 多重編輯,且命令順序會影響結果 |
-f | 指定一個 sed 腳本文件到命令行執行, |
-r | Sed 使用擴展正則 |
-i | 直接修改文檔讀取的內容,不在屏幕上輸出 |
Sed 操作命令
sed 操作命令告訴 sed 如何處理由地址指定的各輸入行。如果沒有指定地址, sed 就會處理輸入的所有的行。表 3.sed 命令
命 令 | 說 明 |
a\ | 在當前行后添加一行或多行 |
c\ | 用新文本修改(替換)當前行中的文本 |
d | 刪除行 |
i\ | 在當前行之前插入文本 |
h | 把模式空間里的內容復制到暫存緩存區 |
H | 把模式空間里的內容追加到暫存緩存區 |
g | 取出暫存緩沖區里的內容,將其復制到模式空間,覆蓋該處原有內容 |
G | 取出暫存緩沖區里的內容,將其復制到模式空間,追加在原有內容后面 |
l | 列出非打印字符 |
p | 打印行 |
n | 讀入下一輸入行,並從下一條命令而不是第一條命令開始處理 |
q | 結束或退出 sed |
r | 從文件中讀取輸入行 |
! | 對所選行意外的所有行應用命令 |
s | 用一個字符串替換另一個 |
表 4.替換標志
g | 在行內進行全局替換 |
p | 打印行 |
w | 將行寫入文件 |
x | 交換暫存緩沖區與模式空間的內容 |
y | 將字符轉換為另一字符(不能對正則表達式使用 y 命令) |
報錯信息和退出信息
遇到語法錯誤時, sed 會向標准錯誤輸出發送一條相當簡單的報錯信息。但是,如果 sed判斷不出錯在何處,它會“斷章取義”,給出令人迷惑的報錯信息。如果沒有語法錯誤, sed將會返回給 shell 一個退出狀態,狀態為 0 代表成功,為非 0 整數代表失敗。
sed使用實例
下面給出測試文件作為輸入文件:
1
2
3
4
5
6
7
8
9
10
|
[root@Gin scripts]
# cat ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
|
打印: p 命令
命令 p 是打印命令,用於顯示模式緩存區的內容。默認情況下, sed 把輸入行打印在屏幕上,選項-n 用於取消默認打印操縱。當選項-n 和命令 p 同時出現時, sed 可打印選定的內容
案例1:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@Gin scripts]
# sed '/north/p' ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
|
說明:默認情況下, sed 把所有輸入行都打印在標准輸出上。如果在某一行匹配到 north, sed就把該行另外打印一遍。
案例2:
1
2
3
4
|
[root@Gin scripts]
# sed -n '/north/p' ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
|
說明:默認情況下, sed 打印當前緩存區中的輸入行。命令 p 指示 sed 將再次打印該行。選項-n 取消 sed 取消默認打印操作。選線-n 和命令配合使用,模式緩沖區內的輸入行,只被打印一次。如果不指定-n 選項, sed 就會像上例中那樣,打印出重復的行。如果指定了-n,則sed 只打印包含模式 north 的行。
刪除: d 命令
命令 d 用於刪除輸入行。sed 先將輸入行從文件復制到模式緩存區,然后對該行執行 sed命令,最后將模式緩存區的內容顯示在屏幕上。如果發出的是命令 d,當前模式緩存區的輸入行會被刪除,不被顯示。
案例 3:
1
2
3
4
5
6
7
8
9
|
[root@Gin scripts]
# sed '3d' ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
|
說明:刪除第 3 行。默認情況下,其余的行都被打印到屏幕上。
案例 4:
1
2
3
|
[root@Gin scripts]
# sed '3,$d' ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
|
說明:刪除從第三行到最后一行內容,剩余各行被打印。地址范圍是開始第 3 行,結束最后一行。
案例 5:
1
2
3
4
5
6
7
|
[root@Gin scripts]
# sed '/north/d' ceshi.txt
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
central CT Ann Stephens 5.7 .94 5 13
|
說明:所有包含模式 north 的行都被動刪除,其余行被打印。
替換: s 命令
命令 s 是替換命令。替換和取代文件中的文本可以通過 sed 中的 s 來實現, s 后包含在斜杠中的文本是正則表達式,后面跟着的是需要替換的文本。可以通過 g 標志對行進行全局替換
案例 6:
1
2
3
4
5
6
7
8
9
10
|
[root@Gin scripts]
# sed 's/west/north/g' ceshi.txt
northnorth NW Charles Main 3.0 .98 3 34
northern WE Sharon Gray 5.3 .97 5 23
southnorth SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
|
說明:s 命令用於替換。命令末端的 g 表示在行內全局替換;也就是說如果每一行里出現多個west,所有的 west 都會被替換為 north。如果沒有 g 命令,則只將每一行的第一 west 替換為 north。
案例 7:
1
2
|
[root@Gin scripts]
# sed -n 's/^west/north/p' ceshi.txt
northern WE Sharon Gray 5.3 .97 5 23
|
說明:s 命令用於替換。選線-n 與命令行末尾的標志 p 結合,告訴 sed 只打印發生替換的那些行;也就是說,如果只有在行首找到 west 並替換成 north 時才會打印此行。
案例 8:
1
2
3
4
5
6
7
8
9
10
|
[root@Gin scripts]
# sed 's/[0-9][0-9]$/&.5/' ceshi.txt
northwest NW Charles Main 3.0 .98 3 34.5
western WE Sharon Gray 5.3 .97 5 23.5
southwest SW Lewis Dalsass 2.7 .8 2 18.5
southern SO Suan Chin 5.1 .95 4 15.5
southeast SE Patricia Hemenway 4.0 .7 4 17.5
eastern EA TB Savage 4.4 .84 5 20.5
northeast NE AM Main Jr. 5.1 .94 3 13.5
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13.5
|
說明:當“與”符號( &)用在替換串中時,它代表在查找串中匹配到的內容時。這個示例中所有以 2 位數結尾的行后面都被加上.5。
案例 9:
1
2
|
[root@Gin scripts]
# sed -n 's/Hemenway/Jones/gp' ceshi.txt
southeast SE Patricia Jones 4.0 .7 4 17
|
說明:文件中出現的所有的 Hemenway 都被替換為 Jones,只有發生變化的行才會打印出來。選項-n 與命令 p 的組合取消了默認的輸出。標志 g 的含義是表示在行內全局替換。
案例 10:
1
2
3
4
5
6
7
8
9
10
11
|
[root@Gin scripts]
# sed 's/\(Mar\)got/\1linanne/p' ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Marlinanne Weber 4.5 .89 5 9
north NO Marlinanne Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
|
說明:包含在圓括號里的模式 Mar 作為標簽 1 保存在特定的寄存器中。替換串可以通過\1 來引用它。則 Margot 被替換為 Marlinane。
案例 11:
1
2
3
4
5
6
7
8
9
10
|
[root@Gin scripts]
# sed 's#3#88#g' ceshi.txt
northwest NW Charles Main 88.0 .98 88 884
western WE Sharon Gray 5.88 .97 5 288
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 88 188
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 188
|
說明:緊跟在 s 命令后的字符就是查找串和替換串之間的分隔符。分隔符默認默認為正斜杠,但可以改變。無論什么字符(換行符,反斜線除外),只要緊跟在 s 命令,就成了新的串分隔符。這個方法在查找包含正斜杠模式時很管用,例如查找路徑名或生日。
指定行的范圍:逗號
行的范圍從文件中的一個地址開始,在另一個地址結束。地址范圍可以是行號(例如5,10),正則表達式(例如/Dick/和/Joe/),或者兩者的結合(例如/north/,$)范圍是閉合的——包含開始條件的行,結束條件的行,以及兩者之間的行。如果結束條件無法滿足,就會一直操作到文件結尾。如果結束條件滿足,則繼續查找滿足開始條件的位置,范圍重新開始。
案例 12:
1
2
3
4
5
6
|
[root@Gin scripts]
# sed -n '/west/,/east/p' ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
|
說明:打印模式 west 和 east 之間所有的行。如果 west 出現在 east 之后的某一行,則打印的范圍從 west 所在行開始,到下一個出現 east 的行或文件的末尾(如果前者未出現)。圖中用箭頭表示出了該范圍。
案例 13:
1
2
3
4
|
[root@Gin scripts]
# sed -n '5,/northeast/p' ceshi.txt
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
|
說明:打印從第 5 行開始第一個以 northeast 開頭的行之間的所有行。
案例 14:
1
2
3
4
5
6
7
8
9
10
|
[root@Gin scripts]
# sed '/west/,/east/s/$/**VACA**/' ceshi.txt
northwest NW Charles Main 3.0 .98 3 34**VACA**
western WE Sharon Gray 5.3 .97 5 23**VACA**
southwest SW Lewis Dalsass 2.7 .8 2 18**VACA**
southern SO Suan Chin 5.1 .95 4 15**VACA**
southeast SE Patricia Hemenway 4.0 .7 4 17**VACA**
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
|
說明:修改從模式 wast 和 east 之間的所有行,將各行的行尾($)替換為字符串**VACA**。換行符被移到新的字符串后面。
多重編輯: e 命令
-e 命令是編輯命令,用於 sed 執行多個編輯任務的情況下。在下一行開始編輯前,所有的編輯動作將應用到模式緩存區的行上。
案例 15:
1
2
3
4
5
6
7
|
[root@Gin scripts]
# sed -e '1,3d' -e 's/Hemenway/Jones/' ceshi.txt
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Jones 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
|
說明:選項-e 用於進行多重編輯。第一重編輯編輯刪除第 1~3 行。第二重編輯將Hemenway 替換為 Jones。因為是逐行進行這兩行編輯(即這兩個命令都在模式空間的當前行上執行),所以編輯命令的順序會影響結果。例如,如果兩條命令都執行的是替換,前一次替換會影響后一次替換。
追加: a 命令
a 命令是追加命令,追加將新文本到文件中當前行(即讀入模式的緩存區行)的后面。不管是在命令行中,還是在 sed 腳本中, a 命令總是在反斜杠的后面。
案例 16:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@Gin scripts]
# sed '/^north/a Hello world!' ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
Hello world!
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
Hello world!
north NO Margot Weber 4.5 .89 5 9
Hello world!
central CT Ann Stephens 5.7 .94 5 13
|
說明:命令 a 用於追加。字符串 Hello, World!被加在以 north 開頭的各行之后。如果要追加的內容超過一行,則除最后一行外,其他各行都必須以反斜杠結尾。
插入: i 命令
i 命令是插入命令,類似於 a 命令,但不是在當前行后增加文本,而是在當前行前面插入新的文本,即剛讀入緩存區模式的行。
案例 17:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@Gin scripts]
# sed '/eastern/i Hello,world!\
> -----------------------' ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
Hello,world!
-----------------------
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
|
說明:命令 i 是插入命令。如果在某一行匹配到模式 eastern,i 命令就在該行的上方插入命令中插入反斜杠后面后的文本。除了最后一行,
修改: c 命令
c 命令是修改命令。 sed 使用該命令將已有的文本修改成新的文本。舊文本被覆蓋。
案例 18:
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@Gin scripts]
# sed '/eastern/c Hello,world! \
> ------------------' ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
Hello,world!
------------------
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
|
說明:c 命令是修改命令。該命令將完整地修改在模式緩沖區行的當前行。如果模式 eastern被匹配, c 命令將其后的文本替換包含 eastern 的行。
獲取下一行: n 命令
n 命令表示下一條命令。 sed 使用該命令獲取輸入文件的下一行,並將其讀入到模式緩沖區中,任何 sed 命令都將應用到匹配行,緊接着的下一行上。
案例 19:
1
2
3
4
5
6
7
8
9
10
|
[root@Gin scripts]
# sed '/eastern/{n;s/AM/Archie/;}' ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE Archie Main Jr. 5.1 .94 3 13
## 此行就是被替換的行
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
|
說明:如果在某一行匹配到模式 eastern, n 命令就指示 sed 用下一個輸入行(即包含 AM MainJr 的那行)替換模式空間中的當前行,並用 Archie 替換 AM,然后打印該行,再繼續往下處理
轉換: y,命令
y 命令表示轉換。該命令與 tr 命令相似,字符按照一對一的方式從左到右進行轉換。例如 y/abc/ABC/,會把小寫字母轉換成大寫字母, a-->A,b-->B,c-->C。
案例 20:
1
2
3
4
5
6
7
8
9
10
|
[root@Gin scripts]
# sed '1,3y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' ceshi.txt
NORTHWEST NW CHARLES MAIN 3.0 .98 3 34
WESTERN WE SHARON GRAY 5.3 .97 5 23
SOUTHWEST SW LEWIS DALSASS 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
|
說明:y 命令把 1~3 行中所有的小寫命令字母都轉換成了大寫。正則表達式元字符對 y 命令不起作用。與替分隔符一樣,斜杠可以被替換成其他字符。
退出: q 命令
q 命令表示退出命令。該命令將導致 sed 程序退出,且不再進行其他的處理。
案例 21:
1
2
3
4
5
6
|
[root@Gin scripts]
# sed '5q' ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
|
說明:打印完第 5 行之后, q 讓 sed 程序退出。
案例 22:
1
2
3
4
|
[root@Gin scripts]
# sed '/Lewis/{ s/Lewis/Joseph/;q; }' ceshi.txt
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Joseph Dalsass 2.7 .8 2 18
|
說明:在某行匹配到模式 Lewis 時, s 表示先用 Joseph 替換 Lewis,然后 q 命令讓 sed 退出。
生產環境案例
在實際生產中,在修改配置文件的時候,有一些空格、空行、帶“ #”開頭的注釋都要刪除或替換,下面為大家介紹幾個實用的例子
案例 23:
1
2
3
4
5
|
[root@Gin scripts]
# cat sed.txt
today is
nice
day
you can walk out on the street
it will be
import
to you
##每行的前面都有空格
|
1
2
3
4
5
6
7
8
9
10
|
[root@Gin scripts]
# sed 's/^[ ]*//' sed.txt
today is
nice
day
you can walk out on the street
it will be
import
to you
## 注:[ ]里面有個空格
或者:
[root@Gin scripts]
# sed 's/^[[:space:]]*//' sed.txt
today is
nice
day
you can walk out on the street
it will be
import
to you
|
案例24:刪除文本中空行和空格組成的行及#號注釋的行
1
2
3
4
5
6
7
8
|
[root@Gin scripts]
# grep -Eiv "^#|^$" ssh_config
Host *
GSSAPIAuthentication
yes
ForwardX11Trusted
yes
SendEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
SendEnv LC_IDENTIFICATION LC_ALL LANGUAGE
SendEnv XMODIFIERS
|
案例 25:從 Google 上下載下來的配置文件往往都帶有數字,現在需要刪除所有行的首數字。
1
2
3
4
5
6
7
8
|
[root@Gin scripts]
# cat sed.txt
1today is
nice
day
2you can walk out on the street
3it will be
import
to you
[root@Gin scripts]
# sed 's/^[0-9][0-9]*//g' sed.txt
today is
nice
day
you can walk out on the street
it will be
import
to you
|
總結
本章內容總結了 sed 命令的用法,前面部分是 sed 命令的語法,后面部分則主要以實際案例來說明 sed 的用法,最后面一點介紹了 sed 命令在生產實踐中的運用。所謂學為練,練為戰,希望大家能夠將 sed 命令勤加練習,必將會在工作中有所用途,尤其是頻繁的分析日志文件, Awk+Sed 是比較好的組合。最后希望本文對大家有所幫助,真正達到熟練的程度這就靠大家在工作中歸納總結了。
轉自:https://blog.csdn.net/youmatterhsp/article/details/80208460