【笔记】sed 命令 - 按照规则来编辑数据

外置命令。

[root@localhost ~]# sed [选项] [脚本命令] 文件名
  • sed 命令的选项
选项 功能
-e 脚本命令 该选项会将其后跟的脚本命令添加到已有的命令中
-f 脚本命令文件 该选项会将其后文件中的脚本命令添加到已有的命令中
-n 默认情况下,sed 会在所有的脚本指定执行完毕后,会自动输出处理后的内容,而该选项会屏蔽启动输出,需使用 print 命令来完成输出
-i 此选项会直接修改源文件,要慎用

sed 命令的执行顺序:

  1. 每次仅读取一行内容;
  2. 根据提供的规则命令匹配并修改数据。注意,sed 默认不会直接修改源文件数据,而是会将数据复制到缓冲区中,修改也仅限于缓冲区中的数据;
  3. 将执行结果输出。
    当一行数据匹配完成后,它会继续读取下一行数据,并重复这个过程,直到将文件中所有数据处理完毕。

1. sed 脚本命令

1.1 sed s 替换单词脚本命令

[address]s/pattern/replacement/flags

address 表示指定要操作的具体行,pattern 指的是需要替换的内容,replacement 指的是要替换的新内容。

  • sed s 命令 flags 标记及功能
flags 标记 功能
n 1~512 之间的数字,表示指定要替换的字符串出现第几次时才进行替换,例如,一行中有 3 个 A,但用户只想替换第二个 A,这是就用到这个标记
g 对数据中所有匹配到的内容进行替换,如果没有 g,则只会在第一次匹配成功时做替换操作。例如,一行数据中有 3 个 A,则只会替换第一个 A
p 会打印与替换命令中指定的模式匹配的行。此标记通常与 -n 选项一起使用
w file 将缓冲区中的内容写到指定的 file 文件中
& 用正则表达式匹配的内容进行替换
\n 匹配第 n 个子串,该子串之前在 pattern 中用 () 指定
\ 转义(转义替换部分包含:&、\ 等)
  • 示例
[root@localhost ~]# sed 's/test/trial/2' data4.txt
This is a test of the trial script.
This is the second test of the trial script.
# 可以看到,使用数字 2 作为标记的结果就是,sed 编辑器只替换每行中第 2 次
# 出现的匹配模式
[root@localhost ~]# sed 's/test/trial/g' data4.txt
This is a trial of the trial script.
This is the second trial of the trial script.
# 用新文件替换所有匹配的字符串,可以使用 g 标记
[root@localhost ~]# sed -n 's/test/trial/p' data5.txt
This is a trial line.
# -n 选项会禁止 sed 输出,但 p 标记会输出修改过的行,将二者匹配使用的效果
# 就是只输出被替换命令修改过的行
[root@localhost ~]# sed 's/test/trial/w test.txt' data5.txt
This is a trial line.
This is a different line.
# w 标记会将匹配后的结果保存到指定文件中
[root@localhost ~]# sed 's/\/bin\/bash/\/bin\/csh/' /etc/passwd
# 在使用 s 脚本命令时,替换类似文件路径的字符串会比较麻烦,需要将路径中的
# 正斜线进行转义

1.2 sed d 删除行脚本命令

[address]d

如果需要删除文本中的特定行,可以用 d 脚本命令,它会删除指定行中的所有内容。但使用该命令时要特别小心,如果你忘记指定具体行的话,文件中的所有内容都会被删除。
在默认情况下 sed 并不会修改原始文件,这里被删除的行只是从 sed 的输出中消失了,原始文件没做任何改变。

  • 示例
[root@localhost ~]# sed 'd' data1.txt
# 全部删除
[root@localhost ~]# sed '3d' data6.txt
This is line number 1.
This is line number 2.
This is line number 4.
# 删除 data6.txt 文件内容中的第 3 行
[root@localhost ~]# sed '2,3d' data6.txt
This is line number 1.
This is line number 4.
# 删除 data6.txt 文件内容中的第 2、3行
[root@localhost ~]#sed '/1/,/3/d' data6.txt
This is line number 4.
# 删除第 1~3 行的文本数据
[root@localhost ~]# sed '3,$d' data6.txt
This is line number 1.
This is line number 2.
# 删除 data6.txt 文件内容中第 3 行开始的所有的内容

1.3 sed a 和 i 插入行脚本命令

a 命令表示在指定行的后面附加一行,i 命令表示在指定行的前面插入一行,它们的基本格式完全相同。

[address]a(或 i)\新文本内容
  • 示例
[root@localhost ~]# sed '3i\
>This is an inserted line.' data6.txt
This is line number 1.
This is line number 2.
This is an inserted line.
This is line number 3.
# 将一个新行插入到数据流第三行前
[root@localhost ~]# sed '3a\
>This is an appended line.' data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is an appended line.
This is line number 4.
# 将一个新行附加到数据流中第三行后
[root@localhost ~]# sed '1i\
>This is one line of new text.\
>This is another line of new text.' data6.txt
This is one line of new text.
This is another line of new text.
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
# 将一个多行数据添加到数据流中,只需对要插入或附加的文本中的每一行
# 末尾(除最后一行)添加反斜线即可

1.4 sed c 替换行脚本命令

c 命令表示将指定行中的所有内容,替换成该选项后面的字符串。

[address]c\用于替换的新文本
  • 示例
[root@localhost ~]# sed '3c\
>This is a changed line of text.' data6.txt
This is line number 1.
This is line number 2.
This is a changed line of text.
This is line number 4.
# sed 编辑器会修改第三行中的文本

1.5 sed y 转换字母脚本命令

y 转换命令是唯一可以处理单个字符的 sed 脚本命令。转换命令是一个全局命令,也就是说,它会文本行中找到的所有指定字符自动进行转换,而不会考虑它们出现的位置。

[address]y/inchars/outchars/

转换命令会对 inchars 和 outchars 值进行一对一的映射,即 inchars 中的第一个字符会被转换为 outchars 中的第一个字符,第二个字符会被转换成 outchars 中的第二个字符...这个映射过程会一直持续到处理完指定字符。如果 inchars 和 outchars 的长度不同,则 sed 会产生一条错误消息。

  • 示例
[root@localhost ~]# sed 'y/123/789/' data8.txt
This is line number 7.
This is line number 8.
This is line number 9.
This is line number 4.
This is line number 7 again.
This is yet another line.
This is the last line in the file.
# inchars 模式中指定字符的每个实例都会被替换成 outchars 模式中相
# 同位置的那个字符

1.6 sed p 打印行脚本命令

p 命令表示搜索符号条件的行,并输出该行的内容。p 命令常见的用法是打印包含匹配文本模式的行。

[address]p
  • 示例
[root@localhost ~]# sed -n '/number 3/p' data6.txt
This is line number 3.
# 用 -n 选项和 p 命令配合使用,我们可以禁止输出其他行,只打印包含
# 匹配文本模式的行
[root@localhost ~]# sed -n '/3/{
>p
>s/line/test/p
>}' data6.txt
This is line number 3.
This is test number 3.
# sed 命令会查找包含数字 3 的行,然后执行两条命令。首先,脚本用 p 命
# 令来打印出原始行;然后它用 s 命令替换文本,并用 p 标记打印出替换结
# 果。输出同时显示了原来的行文本和新的行文本

1.7 sed w 写入行脚本命令

w 命令用来将文本中指定行的内容写入文件中。通过使用 w 脚本命令,sed 可以实现将包含文本模式的数据行写入目标文件。

[address]w filename

这里的 filename 表示文件名,可以使用相对路径或绝对路径,但不管是哪种,运行 sed 命令的用户都必须有文件的写权限。

  • 示例
[root@localhost ~]# sed '1,2w test.txt' data6.txt
# 将数据流data6.txt中的前两行打印到一个文本文件test.txt中
[root@localhost ~]# cat data11.txt
Blum, R       Browncoat
McGuiness, A  Alliance
Bresnahan, C  Browncoat
Harken, C     Alliance
[root@localhost ~]# sed -n '/Browncoat/w Browncoats.txt' data11.txt
[root@localhost ~]# cat Browncoats.txt
Blum, R       Browncoat
Bresnahan, C  Browncoat
# 将数据流data11.txt中符合 Browncoat 行打印到一个文本文
# 件Browncoats.txt中

1.8 sed r 插入全文件脚本命令

r 命令用于将一个独立文件的数据插入到当前数据流的指定位置。

[address]r filename
  • 示例
[root@localhost ~]# cat data12.txt
This is an added line.
This is the second added line.
[root@localhost ~]# sed '3r data12.txt' data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is an added line.
This is the second added line.
This is line number 4.
# 会将data12.txt文件中的内容插入到data6.txt中第3行的后面
[root@localhost ~]# sed '$r data12.txt' data6.txt
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is an added line.
This is the second added line.
# 会将data12.txt文件中的内容插入到data6.txt中末尾,想将指定文件
# 中的数据插入到数据流的末尾,可以使用 $ 地址符

1.9 sed q 退出脚本命令

q 命令的作用是使 sed 命令在第一次匹配任务结束后,退出 sed 程序,不再进行对后续数据的处理。

  • 示例
[root@localhost ~]# sed '2q' test.txt
This is line number 1.
This is line number 2.
# 在打印输出第 2 行之后,就停止了,是 q 命令造成的
[root@localhost ~]# sed '/number 1/{ s/number 1/number 0/;q; }' test.txt
This is line number 0.
# 使用 q 命令之后,sed 命令会在匹配到 number 1 时,将其替换
# 成 number 0,然后直接退出

2. sed 脚本命令的寻址方式

对各个脚本命令来说,address 用来表明该脚本命令作用到文本中的具体行。
默认情况下,sed 命令会作用于文本数据的所有行。如果只想将命令作用于特定行或某些行,则必须写明 address 部分,表示的方法有以下 2 种:

  1. 以数字形式指定行区间。
  2. 用文本模式指定具体行区间。
    以上两种形式都可以使用如下这 2 种格式:
[address]脚本命令
或者
address {
    多个脚本命令
}

2.1 以数字形式指定行区间

当使用数字方式的行寻址时,可以用行在文本流中的行位置来引用。sed 会将文本流中的第一行编号为 1,然后继续按顺序为接下来的行分配行号。在脚本命令中,指定的地址可以是单个行号,或是用起始行号、逗号以及结尾行号指定的一定区间范围内的行。

  • 示例
[root@localhost ~]#sed '2s/dog/cat/' data1.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
# 只修改地址指定的第二行的文本
[root@localhost ~]# sed '2,3s/dog/cat/' data1.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 cat
The quick brown fox jumps over the lazy dog
# 只修改地址指定的第二行到第三行的文本
[root@localhost ~]# sed '2,$s/dog/cat/' data1.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 cat
The quick brown fox jumps over the lazy cat
# 将命令作用到文本中从第二行开始的所有行,可以用特殊地
# 址——美元符($)表示末尾

2.2 用文本模式指定行区间

允许指定文本模式来过滤出命令要作用的行

/pattern/command

注意,必须用正斜线将要指定的 pattern 封起来,sed 会将该命令作用到包含指定文本模式的行上。

  • 示例
[root@localhost ~]# grep demo /etc/passwd
demo:x:502:502::/home/Samantha:/bin/bash
[root@localhost ~]# sed '/demo/s/bash/csh/' /etc/passwd
root:x:0:0:root:/root:/bin/bash
...
demo:x:502:502::/home/demo:/bin/csh
...
# 只修改用户 demo 的默认 shell 的 bash 变成 csh

3. sed 多行命令

sed 包含了三个可用来处理多行文本的特殊命令,分别是:

  1. Next 命令(N):将数据流中的下一行加进来创建一个多行组来处理。
  2. Delete(D):删除多行组中的一行。
  3. Print(P):打印多行组中的一行。
    注意,以上命令的缩写,都为大写。

3.1 N 多行操作命令

N 命令会将下一行文本内容添加到缓冲区已有数据之后(之间用换行符分隔),从而使前后两个文本行同时位于缓冲区中,sed 命令会将这两行数据当成一行来处理。

  • 示例
[root@localhost ~]# sed '/first/{ N ; s/\n/ / }' data2.txt
This is the header line.
This is the first data line. This is the second data line.
This is the last line.
# 查找含有单词 first 的那行文本。文本文件中的两行在 sed 的
# 输出中成了一行
[root@localhost ~]# sed 'N ; s/System Administrator/Desktop User/' data3.txt
On Tuesday, the Linux Desktop User's group meeting will be held.
All Desktop Users should attend.
Thank you for your attendance.
# 用 N 命令将发现第一个单词的那行和下一行合并后,即使短语内出现了换行,
# 你仍然可以找到它,这是因为,替换命令在 System 和 Administrator之间
# 用了通配符(.)来匹配空格和换行符这两种情况。但当它匹配了换行符时,
# 它就从字符串中删掉了换行符,导致两行合并成一行
[root@localhost ~]# sed '
> s/System\nAdministrator/Desktop\nUser/
> N
> s/System Administrator/Desktop User/
> ' data3.txt
On Tuesday, the Linux Desktop
User's group meeting will be held.
All Desktop Users should attend.
Thank you for your attendance.
# 查找单行中短语的替换命令在数据流的最后一行也能正常工作,多行替换命令
# 则会负责短语出现在数据流中间的情况

3.2 D 多行删除命令

sed 不仅提供了单行删除命令(d),也提供了多行删除命令 D,其作用是只删除缓冲区中的第一行,也就是说,D 命令将缓冲区中第一个换行符(包括换行符)之前的内容删除掉。

  • 示例
On Tuesday, the Linux System
Administrator's group meeting will be held.
All System Administrators should attend.
[root@localhost ~]# sed 'N ; /System\nAdministrator/D' data4.txt
Administrator's group meeting will be held.
All System Administrators should attend.
# 文本的第二行被 N 命令加到了缓冲区,因此 sed 命令第一次匹配就是成功,
# 而 D 命令会将缓冲区中,第一个换行符之前(也就是第一行)的数据删除,
# 所以,得到了如上所示的结果
# 程序中空白行
This is the header line.
This is a data line.
# 程序中空白行
This is the last line.
[root@localhost ~]# sed '/^$/{N ; /header/D}' data5.txt
This is the header line.
This is a data line.
# 程序中空白行
This is the last line.
# sed会查找空白行,然后用 N 命令来将下一文本行添加到缓冲区。此时如果缓冲
# 区的内容中含有单词header,则 D 命令会删除缓冲区中的第一行

3.3 P 多行打印命令

同 d 和 D 之间的区别一样,P(大写)命令和单行打印命令 p(小写)不同,对于具有多行数据的缓冲区来说,它只会打印缓冲区中的第一行,也就是首个换行符之前的所有内容。

  • 示例
[root@localhost ~]# sed '/.*/N;P'
aaa
aaa
bbb
ccc
ccc
ddd
eee
eee
fff
# 每次都使用 N 将下一行内容追加到缓冲区内容的后面(用换行符间隔),也就是
# 说,第一次时缓冲区中的内容为 aaa\nbbb,但 P(大写) 命令的作用的打印换
# 行符之前的内容,也就是 aaa,之后则是 sed 在自动输出功能输出 aaa 和 bbb
# (sed 命令会自动将 \n 输出为换行),依次类推,就输出了所看到的结果
[root@localhost ~]# sed '/.*/N;p'
aaa
bbb
aaa
bbb
ccc
ddd
ccc
ddd
eee
fff
eee
fff
# 使用的是 p (小写)单行打印命令,它会将缓冲区中的所有内容全部打印出
# 来(\n 会自动输出为换行),因此,出现了看到的结果

4. sed 保持空间

sed 命令处理的是缓冲区中的内容,其实这里的缓冲区,应称为模式空间。值得一提的是,模式空间并不是 sed 命令保存文件的唯一空间。sed 还有另一块称为保持空间的缓冲区域,它可以用来临时存储一些数据。sed 操作保持空间的命令:

命令 功能
h 将模式空间中的内容复制到保持空间
H 将模式空间中的内容附加到保持空间
g 将保持空间中的内容复制到模式空间
G 将保持空间中的内容附加到模式空间
x 交换模式空间和保持空间中的内容
  • 示例
[root@localhost ~]# cat data2.txt
This is the header line.
This is the first data line.
This is the second data line.
This is the last line.
[root@localhost ~]# sed -n '/first/ {h ; p ; n ; p ; g ; p }' data2.txt
This is the first data line.
This is the second data line.
This is the first data line.

这个例子的运行过程是这样的:

  • sed脚本命令用正则表达式过滤出含有单词first的行。
  • 当含有单词 first 的行出现时,h 命令将该行放到保持空间。
  • p 命令打印模式空间也就是第一个数据行的内容。
  • n 命令提取数据流中的下一行(This is the second data line),并将它放到模式空间。
  • p 命令打印模式空间的内容,现在是第二个数据行。
  • g 命令将保持空间的内容(This is the first data line)放回模式空间,替换当前文本。
  • p 命令打印模式空间的当前内容,现在变回第一个数据行了。

5. sed 改变指定流程

5.1 b 分支命令

通常,sed 程序的执行过程会从第一个脚本命令开始,一直执行到最后一个脚本命令(D 命令是个例外,它会强制 sed 返回到脚本的顶部,而不读取新的行)。sed 提供了 b 分支命令来改变命令脚本的执行流程,其结果与结构化编程类似。

[address]b [label]

其中,address 参数决定了哪些行的数据会触发分支命令,label 参数定义了要跳转到的位置。如果没有加 label 参数,跳转命令会跳转到脚本的结尾。

  • 示例
[root@localhost ~]# cat data2.txt
This is the header line.
This is the first data line.
This is the second data line.
This is the last line.
[root@localhost ~]# sed '{2,3b ; s/This is/Is this/ ; s/line./test?/}' data2.txt
Is this the header test?
This is the first data line.
This is the second data line.
Is this the last test?
# 因为 b 命令未指定 label 参数,因此数据流中的第2行和第3行并
# 没有执行那两个替换命令
[root@localhost ~]# sed '{/first/b jump1 ; s/This is the/No jump on/
> :jump1
> s/This is the/Jump here on/}' data2.txt
No jump on header line
Jump here on first data line
No jump on second data line
No jump on last line
# 如果文本行中出现了 first,程序的执行会直接跳到 jump1 标签之后的
# 脚本行。如果分支命令的模式没有匹配,sed 会继续执行所有的脚本命令
[root@localhost ~]# echo "This, is, a, test, to, remove, commas." | sed -n '{
> :start
> s/,//1p
> /,/b start
> }'
This is, a, test, to, remove, commas.
This is a, test, to, remove, commas.
This is a test, to, remove, commas.
This is a test to, remove, commas.
This is a test to remove, commas.
This is a test to remove commas.
# 当缓冲区中的行内容中有逗号时,脚本命令就会一直循环执行,每次迭代都
# 会删除文本中的第一个逗号,并打印字符串,直至内容中没有逗号

5.2 t 测试命令

类似于 b 分支命令,t 命令也可以用来改变 sed 脚本的执行流程。t 测试命令会根据 s 替换命令的结果,如果匹配并替换成功,则脚本的执行会跳转到指定的标签;反之,t 命令无效。在没有指定标签的情况下,如果 s 命令替换成功,sed 会跳转到脚本的结尾(相当于不执行任何脚本命令)。

[address]t [label]
  • 示例
[root@localhost ~]# sed '{
> s/first/matched/
> t
> s/This is the/No match on/
> }' data2.txt
No match on header line
This is the matched data line
No match on second data line
No match on last line
# 第一个替换命令会查找模式文本 first,如果匹配并替换成功,命令会直接跳
# 过后面的替换命令;反之,如果第一个替换命令未能匹配成功,第二个替换命
# 令就会被执行
[root@localhost ~]#  echo "This, is, a, test, to, remove, commas. " | sed -n '{
> :start
> s/,//1p
> t start
> }'
This is, a, test, to, remove, commas.
This is a, test, to, remove, commas.
This is a test, to, remove, commas.
This is a test to, remove, commas.
This is a test to remove, commas.
This is a test to remove commas.

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,392评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,258评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,417评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,992评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,930评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,199评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,652评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,327评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,463评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,382评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,432评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,118评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,704评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,787评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,999评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,476评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,057评论 2 341