Sed的 man手冊參數詳細解釋(九)

P(小寫) Print the current pattern space.

打印當前模式空間(Pattern space)中的全部內容。

P(大寫) Print up to the first embedded newline of the current pattern space.

打印當前模式空間(Pattern space)中內容的第一行數據。
提示:之前說過模式空間是可以存在多行數據的,p(小寫)和P(大寫)作用不同在於,前者打印全部,而後者只打印第一行。
/ +++++++++++++++++++++++++++++++++++++++例子27+++++++++++++++++++++++++++++++++++++

sed.txt的內容爲:

This is a dog.

This is a cat.
sed命令:

sed -ne ' #按Enter鍵換行

>1{ #選擇第一行

>N #將第二行追加到當前行

>P #大寫P,打印當前模式空間的第一行

>}

>' sed.txt

執行以後顯示結果爲:

This is a dog.
將以上命令中的大寫P換成小寫p 執行後顯示結果爲:

This is a dog.

This is a cat.

/++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

s/regexp/replacement/

Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement. The replacement may contain the special character & to refer to that portion of the pattern space which matched, and the special escapes /1 through /9 to refer to the corresponding matching sub-expressions in the regexp.

嘗試對模式空間中的內容進行正則表達式“regexp”的匹配,如果匹配成功,將會用“replacement”來代替匹配的部分;“replacement”可以包含特殊字符“&”,“&”用來代替匹配“regexp”的模式空間的內容,而/1、/2、/3……/9表示“regexp”的子表達式的匹配內容。

提示:1、2、3……等數據第幾個子表達式的意思。
/ +++++++++++++++++++++++++++++++++++++++例子28+++++++++++++++++++++++++++++++++++++

sed.txt的內容爲:
It is my dog,and your dog is there.
sed命令:sed -ne 's//(.*/)/(dog/)/(.*/)/(dog//(.*/)//1horse/3bird/5/g;p' sed.txt
執行結果:

It is my horse,and your bird is there.
其中/1代表第一個/(.*/)匹配的內容,這裏匹配:It is my ;/3代表第二個/(.*/)匹配的內容,這裏匹配的是:,and your ;/5代表第三個/(.*/)匹配的內容,這裏匹配的是:is there.。

/++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

w filename

Write the current pattern space to filename.

將當前模式空間(Pattern space)的全部內容寫入到文件“filename”中

W filename

Write the first line of the current pattern space to filename.

將當前模式空間(Pattern space)內容中的第一行寫入到“filename”中。
/ +++++++++++++++++++++++++++++++++++++++例子29+++++++++++++++++++++++++++++++++++++

sed.txt的內容爲:

sed.txt的內容爲:

This is a dog.

This is a cat.
sed命令:
sed -ne ' #按Enter鍵換行

>1{ #選擇第一行

>N #將下一行追加到當前模式空間,並用換行符隔開

>w sed.write #將當前模式空間中的內容全部寫入sed.write中

>}

>' sed.txt

執行以上命令sed.write的內容爲:

This is a dog.

This is a cat.

如果將以上命令中的w換成W執行以後,sed.write的內容爲:

This is a dog.

/++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

y/source/dest/

Transliterate the characters in the pattern space which appear in source to the corresponding character in dest.

用另一種字母體系將模式空間(Patter space)內出現的“source”字符替換成對應的“dest”字符。
提示:這個命令說白了就是轉換數據中的字元,例如指令:y/abc.../xyz.../針對這個指令 /abc.../xyz.../(x、y、z、a、b、c 代表某些字元)爲y的參數,其中 abc... 與 xyz... 的字元個數必須相同;sed 執行轉換時,將 模式空間內數據中的 a 字元轉換成 x 字元 、b 字元轉換成 y 字元 、c 字元轉換成 z 字元……
/ +++++++++++++++++++++++++++++++++++++++例子30+++++++++++++++++++++++++++++++++++++

sed.txt的內容爲:

sed.txt的內容爲:

This is a dog.

This is a cat.
sed命令爲:

sed -ne ' #按Enter鍵換行

>y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/ #將小寫字母換成大寫字母

>p #打印當前模式空間的內容

>' sed.txt

執行以後結果爲:

THIS IS A DOG.

THIS IS A CAT.

注意那兩個元字符集個數要一樣,而且對應才能互相轉換。

/++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

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