1. 程式人生 > 實用技巧 >linux工具-sed

linux工具-sed

目錄

1. 基本語法

定義:sed:Stream Editor文字流編輯.

語法格式:

sed [option] '[address-range| pattern-to-match] sed-command' filename
sed [option] -f sed_script_file filename

2. option

sed的主要option如下:

選項 說明
-n 只打印模式匹配的行,預設情況下sed會把檔案中所有行都打印出來.
-e 直接在命令列模式上進行sed的動作編輯,即sed動作寫在命令列,不使用指令碼,與-f選項相對應. 此為預設選項.
-f sed動作寫在一個指令碼檔案內,用-f file.sed來執行sed動作
-r 支援擴充套件的正則表示式.
-i 直接修改檔案內容,預設情況下sed只修改輸出到Terminal的內容.

3. sed動作

格式:'[address-range| pattern-to-match] sed-command'

sed動作分兩部分 作用
address-range或pattern-to-match 指定檔案中的一系列文字行,可以是特定範圍內的行,也可以是匹配某模式的行.
sed-command 對上述的文字行進行處理.

注意:

  1. sed動作要寫在一對單引號中,防止被shell解釋為其它命令.
  2. sed動作的兩部分之間可以有空格,也可以沒空格.
  3. address-range| pattern-to-match是可選的,如果不指定,預設是檔案中的所有行.

3.1. address-range| pattern-to-match

作用:指定檔案中的一系列文字行,預設為文字中的所有文字行.

格式:address-range可以通過行號指定,也可以通過/pattern/指定

addr-range or pattern 說明
m 處理第m行,m為行號
/pattern/ 處理匹配/pattern/的行
m, n 處理m到n範圍內的所有行,m/n為行號,逗號兩邊可以加空格
/pattern1/, /pattern2/ 處理匹配/pattern1/和/pattern2/範圍內的所有行
m, /pattern/ 處理第m行~匹配/pattern/範圍內的所有行.
/pattern/, n 處理匹配/pattern/~第n行範圍內的所有行.
m, n! ??

例子:

檔案內容:

$ cat a.txt # 檔案內容  
line first  
line second  
line third  
line forth  
line fifth  
line sixth  
line seventh  

通過行號指定行

$ sed -n '3 p' a.txt     # 列印第3行到Terminal  
line third  
$ sed -n '3,5 p' a.txt   # 列印第3~5行到Terminal  
line third  
line forth  
line fifth  

通過pattern指定行

$ sed -n '/ne th/,/ne fi/ p' a.txt # 列印匹配 /ne th/和/ne fi/ 之間的行到Terminal  
line third  
line forth  
line fifth  
$  
$ sed -n '3,/ne fi/ p' a.txt   
line third  
line forth  
line fifth  
$ sed -n '/ne th/,5 p' a.txt  
line third  
line forth  
line fifth  

反選,指定3~5行之外的行.

$ sed -n '3,5! p' a.txt  # 列印第3~5行之外的行到Terminal  
line first  
line second  
line sixth  
line seventh  

如果第二個pattern沒匹配到,則列印到檔案結尾

$ sed -n '/ne th/,/xxxx/ p' a.txt # 如果第二個pattern沒匹配到,則列印到檔案結尾  
line third  
line forth  
line fifth  
line sixth  
line seventh  

如果第一個pattern沒匹配到,則匹配結果為空

$ sed -n '/xxxx/,/ne fi/ p' a.txt # 如果第一個pattern沒匹配到,則匹配結果為空  
$   

3.2. sed-command

作用:對文字行進行處理.

常用sed命令如下:

命令 說明
p 列印匹配行(和-n選項配合使用)
= 顯示檔案行號
a\text 新增,在定位到的每一行後面附加新行
i\text 新增,在定位到的每一行前面附加新行
d 刪除,定位到的行刪除
c\text 替換,用新文字替換舊文字,
w filename 將文字寫到filename檔案中,類似於輸出重定向
r filename 從檔案filename中讀文字,類似於輸入重定向
s/pattern/txt/ 替換文字
q 第一個模式匹配後即出 或 立即退出
{命令;命令…} 執行多個命令,使用分號分隔開.
...

例子:

檔案內容:

$ cat a.txt # 檔案內容  
line first  
line second  
line third  
line forth  
line fifth  
line sixth  
line seventh  

使用p命令,列印文字

$ sed -n '/ne th/,/ne fi/ p' a.txt  
line third  
line forth  
line fifth  

使用=命令,列印行號

$ sed -n '/ne th/,/ne fi/ =' a.txt  
3  
4  
5  

使用a命令,在每個匹配行後面追加文字

$ sed  '/ne th/,/ne fi/ a\new' a.txt  
line first  
line second  
line third  
new  
line forth  
new  
line fifth  
new  
line sixth  
line seventh  

使用i命令,在每個匹配行前面追加文字

$ sed  '/ne th/,/ne fi/ i\new' a.txt  
line first  
line second  
new  
line third  
new  
line forth  
new  
line fifth  
line sixth  
line seventh  

使用c命令,將匹配到的address-range行替換為新文字,注意不是每行替換一份,而是整個range替換為一份.

$ sed  '/ne th/,/ne fi/ c\new' a.txt  
line first  
line second  
new  
line sixth  
line seventh  

使用d命令,刪除匹配的行

$ sed  '/ne th/,/ne fi/ d' a.txt  
line first  
line second  
line sixth  
line seventh  

使用s命令,替換匹配行內容的文字

$ sed  '/ne th/,/ne fi/ s/line/Number/' a.txt  
line first  
line second  
Number third  
Number forth  
Number fifth  
line sixth  
line seventh  

4. /pattern/支援的正則

待完成

5.

$ VAR="var0"
$ text="1234,abcd"
$ echo $text | sed 's/abcd/$VAR/' # 在單引號中變數替換不成功
1234,$VAR
$ echo $text | sed "s/abcd/$VAR/" # 在雙引號中變數替換成功
1234,var0
$

待完成