Shell sed

sed

替換
修改第二行的文本
$ sed '2,s/dog/cat' file

修改第二,三行的文本
$ sed '2,3s/dog/cat/' file

修改第二行以後的文本,可以用特殊地址----美元符
$ sed '2,$ s/dog/cat/' file
刪除 d
使用該命令時要特別小心,如果忘記加入尋址模式,流中的所有文本行都會被刪除

刪除第三行
$ sed '3d' file

刪除第二,三行
$ sed '2,3d' file

刪除第三行以後
$ sed '3,$ d' file

$ cat file
This is number 1.
This is number 2.
This is number 3.
This is number 4.

匹配模式也適用於刪除命令,刪除掉匹配指定模式的行
$ sed '/number 1/d' file
This is number 2.
This is number 3.
This is number 4.

也可以使用兩個文本模式來刪除某個區間的行,但是要小心。指定的第一個模式會‘打開’行刪除功能,第二個模式會‘關閉’行刪除功能
$ sed '/1/,/3/d' file
This is number 4.

只要sed在數據流中匹配到了開始模式,刪除功能就會打開,可能會導致意外的結果。
$ cat file
This is number 1.
This is number 2.
This is number 3.
This is number 4.
This is line number 1 again.
This is text you want to keep.
This is the last line in the file

第二個1的行再次觸發了刪除命令。但一直沒有匹配到結束模式,所以整個數據流都被刪除掉了。
$ sed '/1/,/3/d' file
This is line number 4.

插入和附加文本

插入

  • i 會在指定行前增加一個新行
  • a 會在指定行後增加一個新行
$ echo "Test Line 2" | sed 'i\Test Line1'
Test Line 1
Test Line 2

$ echo "Test Line 2" | sed 'a\Test Line1'
Test Line 2
Test Line 1

$ cat data6.txt
This is line number 1
This is line number 2
This is line number 3
This is line number 4
This is line number 5
This is line number 6

$ sed '3i\This is an inserted line' data6.txt
This is line number 1
This is line number 2
This is an inserted line
This is line number 3
This is line number 4
This is line number 5
This is line number 6

在文件末尾追加數據,用美元符號$ 
$ sed '$ a\This is a new line of text' data6.txt
This is line number 1
This is line number 2
This is line number 3
This is line number 4
This is line number 5
This is line number 6
This is a new line of text



修改行
  • c 修改數據流中整行文本的內容
$ sed '3c\This is a changed line of text' data6.txt
This is line number 1
This is line number 2
This is a changed line of text
This is line number 4
This is line number 5
This is line number 6

$ sed '/number 2/c\This is a changed line of text' data6.txt
This is line number 1
This is a changed line of text
This is line number 3
This is line number 4
This is line number 5
This is line number 6

$ sed '2,3c\This is a changed line of text' data6.txt
This is line number 1
This is a changed line of text
This is line number 4
This is line number 5
This is line number 6
轉換命令
  • y 是唯一可以處理單個字符的sed編輯器命令。
    • y/inchars/outchars
    • 轉換命令會對inchars和outchars值進行一對一映射。
    • inchars中的第一個字符轉換爲outchars中的第一個字符
    • inchars中的第二個字符圍追爲outchars中的第二個字符
    • 如果inchars和outchars的長度不同,sed會產生一條錯誤消息
$ sed 'y/123/789/' data6.txt
This is line number 7
This is line number 8
This is line number 9
This is line number 4
This is line number 5
This is line number 6
使用sed處理文件
  • w 向文件寫入行
  • r 允許將一個獨立文件中的數據插入到數據流中
  • -n 禁止顯示到STDOUT上
向文件行寫入行
$ sed '1,2w text.txt' data6.txt
This is line number 1
This is line number 2
This is line number 3
This is line number 4
This is line number 5
This is line number 6

$ cat text.txt
This is line number 1
This is line number 2

$ cat data11.txt
Blum,   R               Browncoat
McGuiness,      A       Alliance
Bresnahan,      C       Browncoat
Harken, C               Alliance

$ sed -n '/Browncoat/w Browncoat.txt' data11.txt
$ cat Browncoat.txt
Blum,   R               Browncoat
Bresnahan,      C       Browncoat

從文件讀取數據
讀取命令中使用地址區間,只能指定單獨一個行號或文本模式地址。sed會將文本中的文本插入到指定地址後。
$ cat data12.txt
This is an added line.
This is the second added line.

$ sed '3r data12.txt' data6.txt
This is line number 1
This is line number 2
This is line number 3
This is an added line.
This is the second added line.
This is line number 4
This is line number 5
This is line number 6

$ sed '/number 2/r data12.txt' data6.txt
This is line number 1
This is line number 2
This is an added line.
This is the second added line.
This is line number 3
This is line number 4
This is line number 5
This is line number 6


$ cat notice.std
Would the following people:
LIST
please report to the ship's caption

$  sed '/LIST/{
> r data11.txt
> }' notice.std
Would the following people:
LIST
Blum,   R               Browncoat
McGuiness,      A       Alliance
Bresnahan,      C       Browncoat
Harken, C               Alliance
please report to the ship's caption

刪除佔位文本,使用刪除命令 d
$  sed '/LIST/{
> r data11.txt
> d
> }' notice.std
Would the following people:
Blum,   R               Browncoat
McGuiness,      A       Alliance
Bresnahan,      C       Browncoat
Harken, C               Alliance
please report to the ship's caption

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