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

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