sed编辑器基础操作
- 替换命令(substitute)
$ cat data4
This is a test of the test script
This is the second test of the test script
替换命令默认情况下,只替换每行中出现的第一处。要替换不同地方出现的文本必须使用【替换标记】,替换标记会在替换命令字符串之后设置
$ sed 's/test/trial/' data4
This is a trial of the test script
This is the second trial of the test script
替换标记(flags)
格式:s/pattern/replacement/flags
有四种可用的替换标记:
数字:表明新文件将替换第几处模式匹配的地方
g :表明新文件将会替换所有匹配的文本
p : 表明原先行的内容要打印出来
w file :将替换的结果写入到文件中
替换每行中的第2处匹配的文本
$ sed 's/test/trial/2' data4
This is a test of the trial script
This is the second test of the trial script
替换整个文件中的匹配文本
$ sed 's/test/trial/g' data4
This is a trial of the trial script
This is the second trial of the trial script
-n选项会禁止sed编辑器输出,p替换标记会输出修改后的行。二者结合使用就是:只输出被替换命令修改后的行
$ sed -n 's/second/last/p' data4
This is the last test of the test script
- 使用地址
$ cat data1
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
默认情况下,替换命令作用于文本的所有行,若要作用于特定行或某些行,则使用【行寻址】,两种形式的行寻址:
以数字形式表示行区间;
用文本模式过滤器。
1)指定单个行号
$ sed '2s/dog/cat/' data1
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
2)用起始行号,逗号,结尾行号指定一定范围区间的行
$ sed '2,3s/dog/cat/' data1
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy dog
3)从某行开始的所有行,使用特殊地址$符号
sed '2,$s/dog/cat/' data1
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
4)使用文本模式过滤器
格式:/pattern/command (必须用正斜线将指定的pattern封起来)
$ sed '/fox/s/dog/cat/' data1
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
5)命令组合
格式: address{
command1
command2
...
}
$ sed '2{
s/fox/elphent/
s/dog/cat/
}' data1
The quick brown fox jumps over the lazy dog
The quick brown elphent jumps over the lazy cat
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
sed '3,$ {
s/brown/green/
s/lazy/cative/
}' data1
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick green fox jumps over the cative dog
The quick green fox jumps over the cative dog
- 插入(insert)和附加(append)文本
$ cat data6
This is line number 1
This is line number 2
This is line number 3
This is line number 4
插入(insert)命令(i)会在指定行前增加一个新行
附加(append)命令(a)会在指定行后增加一个新行
在第3行前增加一行
sed '3i\
$> This is an inserted line' data6
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
在第3行后增加一行
sed '3a\
$>This is an append line' data6
This is line number 1
This is line number 2
This is line number 3
This is an append line
This is line number 4
- 修改行
修改(change)命令(c),必须在sed命令中单独指定新行
$ cat data6
This is line number 1
This is line number 2
This is line number 3
This is line number 4
替换第二行信息
$ sed '2c\
This is a change line of test' data6
This is line number 1
This is a change line of test
This is line number 3
This is line number 4
使用文本模式过滤器 替换 匹配到 number 3 模式的行
$ sed '/number 3/c
This is a change line of test' data6
This is line number 1
This is line number 2
This is a change line of test
This is line number 4
使用行寻址指定行区间第2行~第3行 ,并进行替换
$ sed '2,3c\
This is a change line of test' data6
This is line number 1
This is a change line of test
This is line number 4
- 删除行
删除(delete)命令(d),可以通过特定行区间、特殊的文件结尾符 cat data6
This is line number 1
This is line number 2
This is line number 3
This is line number 4
删除第3行
$ sed '3d' data6
This is line number 1
This is line number 2
This is line number 4
删除第2、3行
$ sed '2,3d' data6
This is line number 1
This is line number 4
从第3行删除到最后一行
d' data6
This is line number 1
This is line number 2
使用文本模式过滤器,删除 匹配到 number 2模式的行
$ sed '/number 2/d' data6
This is line number 1
This is line number 3
This is line number 4
使用两个文本模式来删除,匹配到number 1模式“开启”删除功能,匹配到number 3模式“关闭”删除功能
$ sed '/number 1/,/number 3/d' data6
This is line number 4
- 使用sed处理文件
1)写入文件,使用w命令向文件中写入行
格式:[address] w filename
address 支持单个行号、文本模式、行区间等寻址方式
filename 可以使用相对路径或绝对路径
将data6中的第1,2行写入到testwrite文件中
$ cat testwrite
This is line number 1
This is line number 2
2)从文件中读取数据
读取(read)命令(r)允许将一个独立的文件中的数据插入到数据流中。sed编辑器会将文件中文本插入到指定地址后
格式:[address] r filename
address 支持单个行号、文本模式。
$ cat testread
This is test read from file and add
将文件中的信息添加到data6文件第3行之后
$ sed '3r testread' data6
This is line number 1
This is line number 2
This is line number 3
This is test read from file and add
This is line number 4
- 转换命令
转换(transform)命令(y)可以处理单个字符
格式:[address]y/inchars/outchars/
转换命令会对inchars和outchars的值进行一对一的映射,inchars的第一个字符会被转换成outchars中的第一个字符,依此类推,直到处理完指定的字符。
$ cat data6
This is line number 1
This is line number 2
This is line number 3
This is line number 4
将1234分别替换为5678
$ sed 'y/1234/5678/' data6
This is line number 5
This is line number 6
This is line number 7
This is line number 8
- sed命令,-i 选项直接修改读取的文件内容,而不是输出到终端。直接对文本文件进行操作