Linux入门linux学习

Linux学习--No.12 sed流编辑器

2020-02-26  本文已影响0人  小贝学生信

sed编辑器又称为流编辑器(stream editor),是根据预先提供的一组规则,实现自动格式化、插入、修改或删除文本元素的命令行编辑器。此外还有一个命令行编辑器叫gawd,下次有机会再整理。

sed操作特点

在流编辑器将所有命令与一行数据匹配完毕后,它会读取下一行数据并重复这个过程。在流编辑器处理完流中的所有数据行后,它就会终止。

cat test1.txt
Tom is a good cat.
#
cat test2.txt
This is a test of the test script.
This is the second test of the test script.
#
cat test3.txt
Tom is a good cat.
Tom is a good cat.
Tom is a good cat.
#
cat test4.txt
Tom is a good cat.
Jerry is a good mouse.
#
cat test5.txt
Tom is a good cat.1
Tom is a good cat.2
Tom is a good cat.3
Tom is a good cat.4

一、替换操作 substitude,s

1、基本用法

sed 's/good/bad/' test1.txt
Tom is a bad cat.

如上,s命令可以把用斜线指定的第二个字符串替换掉第一个字符串。

2、每行执行多处替换

2.1、-e 选项 读取多命令

sed -e 's/Tom/Jerry/; s/cat/mouse' test1.txt

如上,即可实现两个命令均作用到文件的每行数据上;注意多命令间要加分号隔开。
如果不想用分号,还有另外一种写法--

sed -e '
s/Tom/Jerry/
s/cat/mouse/' test1.txt

2.2、-f 选项 读取文件命令

cat script.sed   #加一个后缀,以示区别
s/Tom/Jerry/
s/cat/mouse/
sed -f script.sed test1.txt

3、尾部标记四命令

3.1、一行存在多处目标模式

替换命令默认只替换每行出现的第一处目标,当然可以修改。
s/old/new/n 只替换每行中,第n次出现的目标。

sed 's/test/trial/2' test2.txt   
This is a test of the trial script.
This is the second test of the trial script.

3.2、替换全部

s/old/new/g 顾名思义,替换掉文本中所有的目标。

 sed 's/test/trial/g' test2.txt   
This is a trial of the trial script.
This is the second trial of the trial script.

3.3、只输出修改过的行

-n s/old/new/p 需要搭配选项 -n(禁止输出)

sed -n 's/second/first/p' test2.txt   
This is the first test of the test script.
#这里就只有被修改的第二行被输出

3.4、另存修改行到指定文件

s/old/new/w filename 终端仍有正常输出, 但只有那些包含匹配模式的行才会被保存。

sed 's/second/first/w tmp.txt' test2.txt
This is a test of the test script.
This is the first test of the test script.
cat tmp.txt
This is the first test of the test script.
#这里被修改的第二行被存为单独一个文件

此外也可以直接将当前数据的目标行文本储存为指定文件,命令为w。如下

sed -n '1,2w tmp.txt' test4.txt
cat test5.txt
Tom is a good cat.1
Tom is a good cat.2

4、前部标记寻行命令

上述的替换模式还是逐行寻找,如果想要仅在目标行替换,那么在第一个引号规则首部加上相应的标记。

4.1、数字标记

sed '2s/good/bad/' test3.txt 
Tom is a good cat.
Tom is a bad cat.
Tom is a good cat.
sed '2,3s/good/bad/' test3.txt  #应该是指第2到3行
sed '2,$s/good/bad/' test3.txt  # $美元符通常指代最后一行

4.2、文本模式

sed 'Jerry/s/good/bad/' test3.txt
Tom is a good cat.
Jerry is a bad mouse.

4.3、在目标行执行多处目标替换

sed '1{
s/Tom/Jerry/
s/cat/mouse/
}' test4.txt

5、特殊的替换

这里的内容其实是原来小节的内容,因为没有用到s命令。但是从效果来说,本质上都是替换,因此将其移到这里。

5.1 替换行文本

新行替换旧行

sed '2c\
Jerry is a good mouse.' test3.txt   
Tom is a good cat.
Jerry is a good mouse.
Tom is a good cat.

5.2 替换单个字符

sed 'y/123/789/' test4.txt  
Tom is a good cat.7
Tom is a good cat.8
Tom is a good cat.9
Tom is a good cat.4

二、删除操作 delete,d

sed '2d' test4.txt   #删除第二行
sed '2,4d' test4.txt
sed '/cat.1/d' test4.txt

值得注意的是,单独使用'd'是全部删除的效果

三、添加行文本

sed '3i\
Jerry is a good mouse.' test4.txt   #
#
Tom is a good cat.1
Tom is a good cat.2
Jerry is a good mouse.
Tom is a good cat.3
Tom is a good cat.4
sed '$a\
Jerry is a good mouse.' test4.txt  
#
Tom is a good cat.1
Tom is a good cat.2
Tom is a good cat.3
Tom is a good cat.4
Jerry is a good mouse.
sed '1i\
Jerry is a good mouse.\
Jerry is a good mouse.' test4.txt   
#
Jerry is a good mouse.
Jerry is a good mouse.
Tom is a good cat.1
Tom is a good cat.2
Tom is a good cat.3
Tom is a good cat.4

特殊的插入,将另一个文本文件读入当前数据里。命令为r,如下例

sed '3r test4.txt' test5.txt
#
Tom is a good cat.1
Tom is a good cat.2
Tom is a good cat.3
Tom is a good cat.
Jerry is a good mouse.
Tom is a good cat.4

四、打印输出

1、输出指定行文本

命令为p,即替换标记里的那个p命令;同样需要搭配 -n 选项

sed -n '2p' test4.txt   #仅输出第二行
sed -n '/cat.3/p' test4.txt   #仅输出目标文本模式的行文本
sed -n '/3/{
p
s/good/bad/p
}' test5.txt
#先显示第三行,再修改第三行

2、打印行号

sed '=' test5.txt
1
Tom is a good cat.1
2
Tom is a good cat.2
3
Tom is a good cat.3
4
Tom is a good cat.4

以上是有关sed编辑器的学习内容,此外gawk十一更高级的行编辑器(p404),之后再学习一下。参考教材为《R语言实战(第2版)》

上一篇 下一篇

猜你喜欢

热点阅读