一、本篇从删除行开始讲起
文本替换命令只是sed编辑器多种命令用法之一,如果我们工作当中,需要删除文本中特定的文本行或者字符串,那就要用到sed编辑器的删除命令了.
删除命令为d,它将删除所有与命令匹配的文本行或字符串。使用删除命令要小心,因为在命令执行的过程中包含寻址模式,如果忘记寻址模式,所有文本行都会从文本流中删除。
- $ sed 'd' test2.txt
注意,上述命令我只是掩饰d的用法,加上-i参数,才能真正删除整个文本的内容。切忌,删除前,一定要记得用寻址模式,下面我们演示利用寻址模式,删除文本中特定的行。
- $ sed '3d' test2.txt
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
发现没,文本*有五行,删除了第三行,所以就变成四行了。
再来做个删除特定文本行范围的实例:
- $ sed '2,3d' test2.txt
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
使用特殊的文件结束符,来指定要删除的一段文本行
- $ sed '3,$d' test2.txt
- this is script test, we are test bash shell
- this is script test, we are test bash shell
sed编辑器的模式匹配功能也同样适用于删除命令
- sed '/number 1/d' test2.txt
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
- this is script test, we are test bash shell
这一步要注意,sed编辑器不会处理原始文件,所删除的所有文本行仅从sed编辑器输出中删除,原始文件仍然包含这些“已删除的行”。
再来练习一个删除指定行范围的例子
- $ sed '/1/,/3/d' test2.txt
- this is script test, we are test bash shell
- this is script test, we are test bash shell
二、插入和附加文本
与其他编辑器一样,sed编辑器也可以在数据流中插入和附加文本。两个操作之间有一定的差别,千万不要搞混淆了哦
插入命令(i)在指定行之前添加新的一行
附加命令(a)在指定行之后添加新的一行
这两个命令格式比较容易混淆,不能在单命令行上使用这两个命令,必须单独要插入和附加的行。格式如下:
- sed '[address]command\
- new line'
new line 中的文本出现在sed输出中指定的位置,使用插入命令,文本出现在该数据流文本之前,例子如下:
- $ echo "testing"|sed 'i\
- > this is a test'
- this is a test
- testing
然而,在使用附加命令的时候,文本就出现在该数据流文本之后,示例如下:
- $ echo "testing"|sed 'a\
- > this is a test'
- testing
- this is a test
(a) ,(i)这两个命令,也可以用在数据流内部进行文本插入和附加处理,在特定文本行上处理:
- $ sed '3i\
- > this is a dog line test' test2.txt
- this is script test, we are test bash shell 1
- this is script test, we are test bash shell 2
- this is a dog line test
- this is script test, we are test bash shell 3
- this is script test, we are test bash shell 4
- this is script test, we are test bash shell 5
在第三行,成功插入this is a dog line test
接下来,我们在将this is a dog line test插入到第三行之后:
- $ sed '3a\
- > this is a dog line test' test2.txt
- this is script test, we are test bash shell 1
- this is script test, we are test bash shell 2
- this is script test, we are test bash shell 3
- this is a dog line test
- this is script test, we are test bash shell 4
- this is script test, we are test bash shell 5
如果数据流有多行,而您又需要在此数据流末尾附加新的一行文本,那么你可以使用美元符号($),它表示尾行,示例:
- $ sed '$a\
- > this is a dog line test' test2.txt
- this is script test, we are test bash shell 1
- this is script test, we are test bash shell 2
- this is script test, we are test bash shell 3
- this is script test, we are test bash shell 4
- this is script test, we are test bash shell 5
- this is a dog line test
要插入或附加多行文本,必须要在每一行插入或附加的新文本行之前使用一个反斜杠,直到到达要插入或者附加文本行的最后一行:
- $ sed '1i\
- > this is a dog test line\
- > this is a beautiful test' test2.txt
- this is a dog test line
- this is a beautiful test
- this is script test, we are test bash shell 1
- this is script test, we are test bash shell 2
- this is script test, we are test bash shell 3
- this is script test, we are test bash shell 4
- this is script test, we are test bash shell 5
三、更改行
更改行命令用于更改数据流中整行文本内容,工作方式与插入和附加命令相同,因此必须与其他命令独立执行。示例:
- $ sed '3c\
- > this is new correct line' test2.txt
- this is script test, we are test bash shell 1
- this is script test, we are test bash shell 2
- this is new correct line
- this is script test, we are test bash shell 4
- this is script test, we are test bash shell 5
在本例中,sed编辑器更改了第三行文本内容,我们也可以使用文本模式进行更改:
- $ sed '/number 3/c\
- > this is new right script' test2.txt
- this is script test, we are test bash shell 1
- this is script test, we are test bash shell 2
- this is new right script
- this is script test, we are test bash shell 5
- this is script test, we are test bash shell 5
文本模式更改命令将更改与所有文本模式匹配的文本行:
- $ sed '/number 1/c\
- > this is a beautiful test script' test2.txt
- this is a beautiful test script
- this is script test, we are test bash shell 2
- this is script test, we are test bash shell 3
- this is script test, we are test bash shell 4
- this is script test, we are test bash shell 5
在更改命令中,可以使用地址范围,但是结果却不是预期要达到的结果。
- $ sed '2,3c\
- > this is a beautiful test script' test2.txt
- this is script test, we are test bash shell 1
- this is a beautiful test script
- this is script test, we are test bash shell 4
- this is script test, we are test bash shell 5
执行后,结果却是把2,3两行变成了一行更改后的文本。所以这点要注意!
本文出自 “小任的学习笔记” 博客,请务必保留此出处http://renyudi.blog.51cto.com/544938/879791