Linux -- sed命令(2)

2 使用地址
默認情況下,在sed編輯器中使用的命令會作用於文本數據的所有行。如果只想將命令作用於特定行或某些行,則必須用行尋址(line addressing)。
在sed編輯器中有兩種形式的行尋址:
以數字形式表示行區間
 用文本模式來過濾出行

2.1 數字行尋址
當使用數字方式的行尋址時,可以用行在文本流中的行位置來引用。sed編輯器會將文本流中的第一行編號爲1,然後繼續按順序爲接下來的行分配行號。在命令中指定的地址可以是單個行號,或是用起始行號、逗號以及結尾行號指定的一定區間範圍內的行。

將第三行的的第二個cat替換爲dog

sed ‘3s/cat/dog/2’ test2.txt
cat cat cat cat cat
cat cat cat cat cat
cat dog cat cat cat
cat cat cat cat cat
cat cat cat cat cat

將第三行到第四行的的第二個cat替換爲dog

sed ‘3,4s/cat/dog/2’ test2.txt
cat cat cat cat cat
cat cat cat cat cat
cat dog cat cat cat
cat dog cat cat cat
cat cat cat cat cat

將第三行及往後的的第二個cat替換爲dog

sed ‘3,$s/cat/dog/2’ test2.txt
cat cat cat cat cat
cat cat cat cat cat
cat dog cat cat cat
cat dog cat cat cat
cat dog cat cat cat
2.2 文本行尋址
另一種限制命令作用到哪些行上的方法會稍稍複雜一些。sed編輯器允許指定文本模式來過濾出命令要作用的行,必須用正斜線將要指定的 pattern 封起來。sed編輯器會將該命令作用到包含指定文本模式的行上。

$ cat test4.txt
A has a apple
B has a apple
C has a apple
D has a apple
$ sed ‘/C/s/apple/orange/’ test4.txt
A has a apple
B has a apple
C has a orange
D has a apple
該命令只作用到匹配文本模式的行上。雖然使用固定文本模式能幫你過濾出特定的值,但其作用難免有限。sed編輯器在文本模式中採用了一種稱爲正則表達式(regular expression)的特性來幫助你創建匹配效果更好的模式。正則表達式允許創建高級文本模式匹配表達式來匹配各種數據。這些表達式結合了一系列通配符、特殊字符以及固定文本字符來生成能夠匹配幾乎任何形式文本的簡練模式。

2.3 命令組合
如果需要在單行上執行多條命令,可以用花括號將多條命令組合在一起。sed編輯器會處理地址行處列出的每條命令。

$ sed ‘4{
s/D/E/
s/apple/banana/}’ test4.txt
A has a apple
B has a apple
C has a apple
E has a banana
兩條命令都會作用到該地址上。當然,也可以在一組命令前指定一個地址區間。

3 d刪除命令
$ sed ‘2d’ test4.txt
A has a apple
C has a apple
D has a apple
$ sed ‘2,3d’ test4.txt
A has a apple
D has a apple
$ sed ‘2,$d’ test4.txt
A has a apple
sed ‘/B/,/C/d’ test4.txt
A has a apple
D has a apple
4 i插入&a追加命令
如你所期望的,跟其他編輯器類似,sed編輯器允許向數據流插入和附加文本行。

插入( insert )命令( i )會在指定行前增加一個新行;
附加( append )命令( a )會在指定行後增加一個新行。

使用命令行時兩個反斜槓不加也可以,只是看起來更美觀

$ sed ‘4i\E has a apple’ test4.txt
A has a apple
B has a apple
C has a apple
E has a apple
D has a apple
$ sed ‘4a\E has a apple’ test4.txt
A has a apple
B has a apple
C has a apple
D has a apple
E has a apple

使用次提示符時非結束行的末尾則必須要加反斜槓

$ sed '4a\

E has a apple
F has a apple’ test4.txt
A has a apple
B has a apple
C has a apple
D has a apple
E has a apple
F has a apple

在末尾追加行

$ sed ‘$a\Last one has nothing’ test4.txt
A has a apple
B has a apple
C has a apple
D has a apple
Last one has nothing
5 c修改命令
修改( change )命令允許修改數據流中整行文本的內容。它跟插入和附加命令的工作機制一樣,你必須在 sed 命令中單獨指定新行。

數字行尋址

$ sed ‘4c\D has nothing’ test4.txt
A has a apple
B has a apple
C has a apple
D has nothing

文本行尋址

$ sed ‘/D/c\D has nothing’ test4.txt
A has a apple
B has a apple
C has a apple
D has nothing
6 y轉換命令
轉換( transform )命令( y )是唯一可以處理單個字符的sed編輯器命令。轉換命令格式如下。

[address]y/inchars/outchars/ file

轉換命令會對 inchars 和 outchars 值進行一對一的映射。 inchars 中的第一個字符會被轉換爲 outchars 中的第一個字符,第二個字符會被轉換成 outchars 中的第二個字符。這個映射過程會一直持續到處理完指定字符。如果 inchars 和 outchars 的長度不同,則sed編輯器會產生一條錯誤消息。

$ sed ‘y/ABCD/EFGH/’ test4.txt
E has a apple
F has a apple
G has a apple
H has a apple
轉換命令是一個全局命令,也就是說,它會文本行中找到的所有指定字符自動進行轉換,而不會考慮它們出現的位置。

7 打印
除了用 p 標記和替換命令顯示sed編輯器修改過的行,另外有3個命令也能用來打印數據流中的信息:

p 命令用來打印文本行;
=命令用來打印行號;
l(小寫的L)命令用來列出行。
7.1 p 命令打印行
打印命令最常見的用法是打印包含匹配文本模式的行。

$ cat data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
$ sed -n ‘/number 3/p’ data6.txt
This is line number 3.

在命令行上用 -n 選項,你可以禁止輸出其他行,只打印包含匹配文本模式的行。

也可以用它來快速打印數據流中的某些行。

$ sed -n ‘2,3p’ data6.txt
This is line number 2.
This is line number 3.
如果需要在修改之前查看行,也可以使用打印命令,比如與替換或修改命令一起使用。可以創建一個腳本在修改行之前顯示該行。

$ sed -n '/3/{

p
s/line/test/p
}’ data6.txt
This is line number 3.
This is test number 3.
sed編輯器命令會查找包含數字3的行,然後執行兩條命令。首先,腳本用 p 命令來打印出原始行;然後它用 s 命令替換文本,並用 p 標記打印出替換結果。輸出同時顯示了原來的行文本和新的行文本。

7.2 =命令打印行號
sed編輯器在實際的文本行出現前打印了行號。如果你要在數據流中查找特定文本模式的話,
等號命令用起來非常方便。

$ sed -n '/number 4/{

=
p
}’ data6.txt
4
This is line number 4.
利用 -n 選項,你就能讓sed編輯器只顯示包含匹配文本模式的行的行號和文本。

7.3 l命令列出行
列出( list )命令( l )可以打印數據流中的文本和不可打印的ASCII字符。任何不可打印字符要麼在其八進制值前加一個反斜線,要麼使用標準C風格的命名法(用於常見的不可打印字符),比如 \t ,來代表製表符。

$ cat data9.txt
This line contains tabs.
$ sed -n ‘l’ data9.txt
This\tline\tcontains\ttabs.$

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章