sed简单教程

来源:互联网 发布:有没有5g网络 编辑:程序博客网 时间:2024/06/05 15:53
**sed全名叫stream editor,流编辑器,用程序的方式来编辑文本,相当的hacker啊。sed基本上就是玩正则模式匹配,所以,玩sed的人,正则表达式一般都比较强。**

用s命令替换

我使用下面这段文本进行演示

[root@linhaobin ~]# cat pets.txt
This is my cat
my cat’s name is cat
This is my dog
my dog’s name is dog
This is my fish
my fish’s name is fish
This is my goat
my goat’s name is goat

把其中的my字符串替换成lhb,下面的语句应该很好理解(s表示替换命令,/my/表示匹配my,/lhb/表示把匹配替换成lhb,/g 表示一行上的替换所有的匹配):

[root@linhaobin ~]# sed “s/my/lhb/g” pets.txt
This is lhb cat
lhb cat’s name is cat
This is lhb dog
lhb dog’s name is dog
This is lhb fish
lhb fish’s name is fish
This is lhb goat
lhb goat’s name is goat

如果你要使用单引号,那么你没办法通过\’这样来转义,就有双引号就可以了,在双引号内可以用\”来转义。

注意:上面的sed并没有对文件的内容改变,只是把处理过后的内容输出,如果你要写回文件,你可以使用重定向,如:

[root@linhaobin ~]# sed “s/my/lhb/g” pets.txt > lhb_pets.txt

或者使用-i参数直接修改文件内容

[root@linhaobin ~]# sed -i “s/my/lhb/g” pets.txt

在每一行前面加点东西

[root@linhaobin ~]# sed “s/^/—/g” pets.txt
—This is my cat
— my cat’s name is cat
—This is my dog
— my dog’s name is dog
—This is my fish
— my fish’s name is fish
—This is my goat
— my goat’s name is goat

在每一行后面加点东西

[root@linhaobin ~]# sed “s/$/—/g” pets.txt
This is my cat—
my cat’s name is cat—
This is my dog—
my dog’s name is dog—
This is my fish—
my fish’s name is fish—
This is my goat—
my goat’s name is goat—

介绍一下正则表达式的一些基本的东西

^ 表示一行的开头。如:/^#/ 以#开头的匹配。
$ 表示一行的结尾。如:/}$/ 以}结尾的匹配。
\< 表示词首。
\> 表示词尾。 如:abc> 表示以 abc 结尾的词。
. 表示任何单个字符。
* 表示某个字符出现了0次或多次。
[ ] 字符集合。 如:[abc] 表示匹配a或b或c,还有 [a-zA-Z] 表示匹配所有的26个字符。如果其中有^表示反,如 [^a] 表示非a的字符

指定替换第三行的内容

[root@linhaobin ~]# sed “3s/my/YOUR/g” pets.txt
This is my cat
my cat’s name is cat
This is YOUR dog
my dog’s name is dog
This is my fish
my fish’s name is fish
This is my goat
my goat’s name is goat

指定替换第3到第6行文本的内容

[root@linhaobin ~]# sed “3,6s/my/YOUR/g” pets.txt
This is my cat
my cat’s name is cat
This is YOUR dog
YOUR dog’s name is dog
This is YOUR fish
YOUR fish’s name is fish
This is my goat
my goat’s name is goat

替换每一行的第一个s为S:

[root@linhaobin ~]# sed “s/s/S/1” pets.txt
ThiS is my cat
my cat’S name is cat
ThiS is my dog
my dog’S name is dog
ThiS is my fish
my fiSh’s name is fish
ThiS is my goat
my goat’S name is goat

替换第2行第2个s为S:

root@linhaobin ~]# sed “2s/s/S/2” pets.txt
This is my cat
my cat’s name iS cat
This is my dog
my dog’s name is dog
This is my fish
my fish’s name is fish
This is my goat
my goat’s name is goat

多个匹配

如果我们需要一次替换多个模式,可参看下面的示例:(第一个模式把第一行到第三行的my替换成your,第二个则把第3行以后的This替换成了That)

root@linhaobin ~]# sed ‘1,3s/my/your/g; 3,$s/This/That/g’ pets.txt
This is your cat your cat’s name is cat
This is your dog your dog’s name is dog
That is your fish your fish’s name is fish
That is my goat my goat’s name is goat

上面的命令等价于:(下面使用的是sed -e 的命令行参数)

[root@linhaobin ~]# sed -e ‘1,3s/my/your/g’ -e ‘3,$s/This/That/g’ pets.txt
This is your cat your cat’s name is cat
This is your dog your dog’s name is dog
That is your fish your fish’s name is fish
That is my goat my goat’s name is goat

N命令

N命令 —— 把下一行的内容纳入当成缓冲区做匹配。

下面的的示例会把原文本中的偶数行纳入奇数行匹配,而s只匹配并替换一次,所以,就成了下面的结果:

[root@linhaobin ~]# cat my.txt
This is your cat
my cat’s name is betty
This is your dog
my dog’s name is frank
This is your fish
my fish’s name is george
This is your goat
my goat’s name is adam

上述文件变成:

[root@linhaobin ~]# sed ‘N;s/\n/,/’ my.txt
This is your cat, my cat’s name is betty
This is your dog, my dog’s name is frank
This is your fish, my fish’s name is george
This is your goat, my goat’s name is adam

a命令和i命令

a命令就是append, i命令就是insert,它们是用来添加行的。如:

在第一行前面插入一行:

[root@linhaobin ~]# sed “1 i This is my monkey,my monkey’s name is monkey ” pets.txt
This is my monkey my monkey’s name is monkey
This is my cat ,my cat’s name is cat
This is my dog ,my dog’s name is dog
This is my fish ,my fish’s name is fish
This is my goat ,my goat’s name is goat

在最后一行后追加一行:

[root@linhaobin ~]# sed “$ a This is my monkey\tmy monkey’s name is monkey ” pets.txt
This is my cat my cat’s name is cat
This is my dog my dog’s name is dog
This is my fish my fish’s name is fish
This is my goat my goat’s name is goat
This is my monkey my monkey’s name is monkey

我们可以运用匹配来添加文本:

[root@linhaobin ~]# sed “/my/a ++++” pets.txt
This is my cat my cat’s name is cat
++++
This is my dog my dog’s name is dog
++++
This is my fish my fish’s name is fish
++++
This is my goat my goat’s name is goat
++++

c命令

c命令匹配替换行

把第2行替换掉:

[root@linhaobin ~]# sed “2 c This is my monkey\t my monkey’s name is wukong” pets.txt
This is my cat my cat’s name is cat
This is my monkey my monkey’s name is wukong
This is my fish my fish’s name is fish
This is my goat my goat’s name is goat

上面的命令等价于:

[root@linhaobin ~]# sed “/dog/c This is my monkey\t my monkey’s name is wukong” pets.txt
This is my cat my cat’s name is cat
This is my monkey my monkey’s name is wukong
This is my fish my fish’s name is fish
This is my goat my goat’s name is goat

d命令

d命令删除匹配行

[root@linhaobin ~]# sed ‘2d’ pets.txt
This is my cat my cat’s name is cat
This is my fish my fish’s name is fish
This is my goat my goat’s name is goat
[root@linhaobin ~]# sed ‘2,$d’ pets.txt
This is my cat my cat’s name is cat
[root@linhaobin ~]# sed ‘/fish/d’ pets.txt
This is my cat my cat’s name is cat
This is my dog my dog’s name is dog
This is my goat my goat’s name is goat

p命令

打印匹配行,你可以把这个命令当成grep式的命令

匹配fish并输出,可以看到fish的那一行被打了两遍,这是因为sed处理时会把处理的信息输出
[root@linhaobin ~]# sed ‘/fish/p’ pets.txt
This is my cat my cat’s name is cat
This is my dog my dog’s name is dog
This is my fish my fish’s name is fish
This is my fish my fish’s name is fish

使用-n参数,直接打印出来

[root@linhaobin ~]# sed -n ‘/fish/p’ pets.txt
This is my fish my fish’s name is fish

从第一行打印到匹配fish成功的那一行

[root@linhaobin ~]# sed -n ‘1,/fish/p’ pets.txt
This is my cat my cat’s name is cat
This is my dog my dog’s name is dog
This is my fish my fish’s name is fish