sed 命令详解

sed 即 stream editor,一个简单而强大的文本解析转换工具,1973-1974年期间由贝尔实验室的 Lee E. McMahon 开发,能够完美的配合正则表达式使用。
处理文本时,当前处理的行会先存储在临时缓冲区中,称为模式空间(pattern space),操作完成后再把缓冲区的内容送往屏幕。接着处理下一行,直到文件末尾。文件内容并没有改变,除非使用重定向存储输出。
sed 主要用来自动编辑一个或多个文件,简化对文件的反复操作,编写转换程序等。

1. 使用 s 命令替换

基本使用

sed 默认情况下通过 STDIN 输入流读取数据,可以利用管道符(|)进行重定向。如下:

$ echo "This is a test" | sed 's/test/big test/'
This is a big test

s 替换命令的格式为:s/pattern/replacement/flags ,如下:

$ cat test.txt
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.
$
$ sed 's/dog/cat/' test.txt
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.

使用 s 命令进行替换时,默认只替换每行中的第一个匹配。全局替换需增加 g 选项。

$ sed 's/o/O/g' test.txt
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.

sed 默认只将处理后的内容输出,而不对原文件的内容做任何改动。如需写入文件,可使用重定向:$ sed 's/dog/cat/' test.txt > test2.txt
或通过 -i 选项直接修改文件内容:$ sed -i 's/dog/cat/' test.txt

address

如果需要将命令应用到特定的一行或多行内容,可以使用 line addressing。格式为 [address[,address]][!]command
address 可以是数字模式,也可以通过逗号分隔两个 address 表示一个区间范围。
如只将第2行中的 dog 替换为 cat

$ sed '2s/dog/cat/' test.txt
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.

或者$ sed '1,3s/dog/cat/' test.txt,替换1-3行的内容。
$ sed '2,$s/dog/cat/' test.txt,替换第2行到最后一行的内容。

! 表示完成匹配后是否在该行执行替换命令。加上 ! 表示不执行。如下:

$ cat lines.txt
This is line one
This is line two
This is line three
This is line four
$
$ sed '/one/,2s/line/LINE/' lines.txt
This is LINE one
This is LINE two
This is line three
This is line four
$
$ sed '/one/,2!s/line/LINE/' lines.txt
This is line one
This is line two
This is LINE three
This is LINE four
替换选项

sed 的替换命令除了可以附加 g 选项(全局替换)外,还有更多选项适用于不同的情形。

  • 数字。表示只替换每行中的第 n 个匹配项,如:
$ sed 's/o/O/2' test.txt
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.

$ sed 's/o/O/2g' test.txt 替换每行中从第二个开始的所有匹配项(即替换每行中从第二个 o 开始直到该行行尾的所有 o 字符)

  • p,表示打印处理前的原始内容,结合上 -n 选项(不打印输出)则可以只输出处理过的内容。
$ sed 's/three/3/p' lines.txt
This is line one
This is line two
This is line 3
This is line 3
This is line four
$
$ sed -n 's/three/3/p' lines.txt
This is line 3
  • w,将处理过的内容写入文件
$ sed 's/three/3/w line3.txt' lines.txt
$
$ cat line3.txt
This is line 3
sed 命令选项
  • 删除:d
    $ sed '2,$d' lines.txt 删除 lines.txt 中第2行到最后一行的内容。
    $ sed '/two/d' lines.txt 删除 lines.txt 中包含 two 的行。
  • 追加(行下):a
$ sed '/two/a\
> This is a line behind line 2' lines.txt
This is line one
This is line two
This is a line behind line 2
This is line three
This is line four
  • 插入(行上):i
$ sed '2i\
> This is a line above line 2' lines.txt

在第2行以上插入内容。

  • 修改:c
$ sed '/two/c\
> Line 2' lines.txt

将包含 two 的行修改为 Line 2(整行内容替换为 Line 2)。

  • 字符转换:y
    格式为:[address]y/inchars/outchars/
    输入文件中所有包含在 inchars 中的字符都将替换为 outchars 中对应的字符。
$ cat line_number.txt
This is line 1.
This is line 2.
This is line 3.
This is another line 1.
$
$ sed 'y/123/456/' line_number.txt
This is line 4.
This is line 5.
This is line 6.
This is another line 4.
其他用法
  • 打印输出
    sed 的 p 命令可以达到类似 grep 的效果,结合上 address 功能还可以完成更复杂的筛选打印操作。
$ sed -n 'p' lines.txt
This is line one
This is line two
This is line three
This is line four
$
$ sed -n '2,3p' lines.txt
This is line two
This is line three
$
$sed -n '/two/,/three/p' lines.txt
This is line two
This is line three
  • 多个匹配
    可以通过 -e 选项实现多个匹配的替换
$ sed -e 's/fox/kangaroo/;s/dog/cat/' test.txt
The quick brown kangaroo jumps over the lazy cat.
The quick brown kangaroo jumps over the lazy cat.
The quick brown kangaroo jumps over the lazy cat.
The quick brown kangaroo jumps over the lazy cat.

也可以这样:

$ sed -e '
> s/brown/red/
> s/fox/kangaroo/
> s/dog/cat/' test.txt
The quick red kangaroo jumps over the lazy cat.
The quick red kangaroo jumps over the lazy cat.
The quick red kangaroo jumps over the lazy cat.
The quick red kangaroo jumps over the lazy cat.
  • 从脚本文件中读取命令
$ cat script.sed
s/brown/red/
s/fox/kangaroo/
s/dog/cat
$
$ sed -f script.sed test.txt
The quick red kangaroo jumps over the lazy cat.
The quick red kangaroo jumps over the lazy cat.
The quick red kangaroo jumps over the lazy cat.
The quick red kangaroo jumps over the lazy cat.
  • 可以使用 & 变量表示前面的匹配项,如:
$ sed 's/fox/&es/' test.txt
The quick brown foxes jumps over the lazy dog.
The quick brown foxes jumps over the lazy dog.
The quick brown foxes jumps over the lazy dog.
The quick brown foxes jumps over the lazy dog.

参考书籍和文章:

Linux Command Line and Shell Scripting Bible, 3rd Edition
SED 简明教程 by 陈皓
Linux 命令大全 / SED 命令

点赞