联系我

Sed指令常用操作指北

2020.06.13

前言

sed 全名叫 stream editor,流编辑器,用程序的方式来编辑文本,sed 基本上就是玩正则模式匹配,所以,玩 sed的人,正则表达式一般都比较强。

准备

这里准备一个test.txt文本文件供测试,文件内容如下:

This is my cat
  my cat's name is betty
This is my dog
  my dog's name is frank
This is my fish
  my fish's name is george
This is my goat
  my goat's name is adam

s命令替换

下面s命令将文本中的my全部换为rush

sed "s/my/rush/g" test.txt

输出结果:

This is rush cat
  rush cat's name is betty
This is rush dog
  rush dog's name is frank
This is rush fish
  rush fish's name is george
This is rush goat
  rush goat's name is adam

其中s/my/rush/g分别代表替换指令、待匹配字符串、替换字符串、全局匹配。

注意点:如果你要使用单引号,那么你没办法通过 \’这样来转义,就有双引号就可以了,在双引号内可以用\”来转义。上面的sed并没有对文件的内容改变,只是把处理过后的内容输出,如果你要写回文件,你可以使用重定向,如:

sed "s/my/rush/g" test.txt > test.txt

除以上方式以外,还可以直接使用“-i”参数,如:

sed -i "s/my/rush/g" test.txt 

在每一行最前面加个“#”号可以这样写:

sed "s/^/#/g" test.txt

输出结果:

#This is my cat
#  my cat's name is betty
#This is my dog
#  my dog's name is frank
#This is my fish
#  my fish's name is george
#This is my goat
#  my goat's name is adam

在每一行最后面加个“#”号可以这样写:

sed "s/$/#/g" test.txt

输出结果:

This is my cat#
  my cat's name is betty#
This is my dog#
  my dog's name is frank#
This is my fish#
  my fish's name is george#
This is my goat#
  my goat's name is adam#

以上两个指令就牵涉到正则的知识了,对正则不是很熟悉的同学可以复习翻看以下正则的知识。

接着来看,如果我想只替换第三到第六行的匹配字符串怎么办?可以这样写:

sed "3,6s/my/your/g" test.txt 

输出结果:

This is my cat
  my cat's name is betty
This is your dog
  your dog's name is frank
This is your fish
  your fish's name is george
This is my goat
  my goat's name is adam

只替换每行第一个匹配到的s字符:

sed "s/s/S/1" test.txt 

输出结果:

ThiS is my cat
  my cat'S name is betty
ThiS is my dog
  my dog'S name is frank
ThiS is my fish
  my fiSh's name is george
ThiS is my goat
  my goat'S name is adam

相应的,如果要替换每一行的第二个s就可以这样写:

sed "s/s/S/2" test.txt

输出结果:

This iS my cat
  my cat's name iS betty
This iS my dog
  my dog's name iS frank
This iS my fish
  my fish'S name is george
This iS my goat
  my goat's name iS adam

只替换第一行的第三个以后的 s:

sed "s/s/S/3g" test.txt

输出结果:

This is my cat
  my cat's name is betty
This is my dog
  my dog's name is frank
This is my fiSh
  my fish's name iS george
This is my goat
  my goat's name is adam

多个匹配

如果想同时匹配多个模式怎么办呢,比如现在需求是这样的:将第一个模式把第一行到第三行的 my 替换成 your,第二个则把第 3 行以后的 This 替换成了 That。我们可以这样写:

sed '1,3s/my/your/g; 3,$s/This/That/g' test.txt

输出结果:

This is your cat
  your cat's name is betty
That is your dog
  my dog's name is frank
That is my fish
  my fish's name is george
That is my goat
  my goat's name is adam

同时上面的指令也等价于:

sed -e '1,3s/my/your/g' -e '3,$s/This/That/g' test.txt

此外,还可以使用&当做被匹配的变量,然后就可以在其左右加点什么,比如:

sed 's/my/[&]/g' test.txt

输出结果:

This is [my] cat
  [my] cat's name is betty
This is [my] dog
  [my] dog's name is frank
This is [my] fish
  [my] fish's name is george
This is [my] goat
  [my] goat's name is adam

圆括号匹配

圆括号括起来的正则表达式所匹配的字符串会可以当成变量来使用,sed 中使用的是 \1,\2…

这里我们新建一个文本文件test1.txt做测试。

This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my goat, my goat's name is adam

圆括号匹配指令:

sed 's/This is my \([^,&]*\),.*is \(.*\)/\1:\2/g' test1.txt

输出结果:

cat:betty
dog:frank
fish:george
goat:adam

以上指令的正则比较复杂,稍微拆解一下还是很容易看懂的,注意\1\2和前面的圆括号对应就好了。

sed的命令

a 命令和 i 命令

a 命令就是 append, i 命令就是 insert,它们是用来添加行的。比如我要在test1.txt的第一行前插入一行就是:

sed "1 i This is my monkey, my monkey's name is wukong" test1.txt

输出结果:

This is my monkey, my monkey's name is wukong
This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my goat, my goat's name is adam

如果我要在test1.txt最后一行的后面插入一行就是:

sed "$ a This is my monkey, my monkey's name is wukong" test1.txt
输出结果:
```php
This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my goat, my goat's name is adam
This is my monkey, my monkey's name is wukong

可以运用匹配来添加文本(匹配到 /fish/ 后就追加一行)

sed "/fish/a This is my monkey, my monkey's name is wukong" test1.txt
输出结果:
```php
This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my monkey, my monkey's name is wukong
This is my goat, my goat's name is adam

c 命令

c 命令是替换匹配行。比如我要在第二行插入内容,就可以这么写:

sed "2 c This is my monkey, my monkey's name is wukong" test1.txt
输出结果:
```php
This is my cat, my cat's name is betty
This is my monkey, my monkey's name is wukong
This is my fish, my fish's name is george
This is my goat, my goat's name is adam

匹配到包含fish的行就替换为新行:

sed "/fish/c This is my monkey, my monkey's name is wukong" test1.txt

输出结果:

This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my monkey, my monkey's name is wukong
This is my goat, my goat's name is adam

d 命令

删除匹配行。比如我要删除匹配上fish的行

sed '/fish/d' test1.txt

输出结果:

This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my goat, my goat's name is adam

删除第二行:

sed '2d' test1.txt

输出结果:

This is my cat, my cat's name is betty
This is my fish, my fish's name is george
This is my goat, my goat's name is adam

删除第二行到最后一行:

sed '2,$d' test1.txt

输出结果:

This is my cat, my cat's name is betty

p 命令

可以把这个命令当成grep命令。比如:

sed '/fish/p' test1.txt

输出结果:

This is my cat, my cat's name is betty
This is my dog, my dog's name is frank
This is my fish, my fish's name is george
This is my fish, my fish's name is george
This is my goat, my goat's name is adam

可以看到包含fish的行被打印了两次,使用 n 参数就好了

sed -n '/fish/p' test1.txt

输出结果:

This is my fish, my fish's name is george