作用:用于解决文本的流编辑器.

命令格局

sed Options '[Address]Command' InputFile

对输出文件 InputFile 中的每行执行给定的命令 Command.

如果指定了地址 Address,则只对符合条件的文本行执行 Command 命令.

罕用选项

  • -i: 原地批改,会扭转源文件中的内容.
  • -n: 处理过程中,不要输入正在解决的文本行.
  • -r: 应用正则表达式扩大.
  • -f File: 从文件 File 中读取 sed 命令.

地址

  • n: 第 n 行.
[ming@localhost test]$ cat data.txt aa123aa456aaa789b123bb456bbb789[ming@localhost test]$ sed -n '2p' data.txt a123

p: 打印行.

  • $: 最初一行.
[ming@localhost test]$ sed -n '$p' data.txt bbb789
  • /regexp/: 匹配指定正则表达式的行.
[ming@localhost test]$ sed -nr '/a+/p' data.txtaa123aa456aaa789[ming@localhost test]$ sed -nr '/a{2,}/p' data.txtaa456aaa789
  • n~m: 从第 n 行开始,每 m 行便执行给定的命令.
[ming@localhost test]$ cat data.txt aa123aa456aaa789b123bb456bbb789[ming@localhost test]$ sed -n '1~2p' data.txt aaa456b123bbb789
  • addr1,addr2: 从 addr1 开始,到 addr2 完结,包含首尾.
[ming@localhost test]$ sed -n '2,4p' data.txta123aa456aaa789[ming@localhost test]$ sed -n '/123/,/123/p' data.txta123aa456aaa789b123
  • addr,+n: 从 addr 开始(包含),直到往后 n 行.
[ming@localhost test]$ sed -n '2,+2p' data.txta123aa456aaa789[ming@localhost test]$ sed -nr '/a{3}/,+2p' data.txtaaa789b123bb456

1. 替换: s/regexp/replacement/

替换每行第一处匹配的中央:将 a 替换成 A

[ming@localhost test]$ sed 's/a/A/' data.txtAA123Aa456Aaa789b123bb456bbb789

替换每行第 2 处匹配的中央:

[ming@localhost test]$ sed 's/a/A/2' data.txtaa123aA456aAa789b123bb456bbb789

替换每行所有匹配的中央:g

[ming@localhost test]$ sed 's/a/A/g' data.txtAA123AA456AAA789b123bb456bbb789

应用正则表达式:

[ming@localhost test]$ sed -r 's/[0-9]+/Number/' data.txtaaNumberaaNumberaaaNumberbNumberbbNumberbbbNumber

援用匹配的内容:&

[ming@localhost test]$ sed -r 's/[0-9]+/<&>/' data.txtaa<123>aa<456>aaa<789>b<123>bb<456>bbb<789>

援用匹配的分组:\n

[ming@localhost test]$ sed -r 's/a{1,3}([0-9]+)/<\1>/' data.txta<123><456><789>b123bb456bbb789

\n: 援用第 n 个分组.

2. 删除行: d

[ming@localhost test]$ sed '/a\+/d' data.txt b123bb456bbb789

3. 插入行: i\a\

插到指定行之前:i\

[ming@localhost test]$ sed -r '/a+/i\line1\n\line2' data.txt line1line2aline1line2a123line1line2aa456line1line2aaa789b123bb456bbb789

插到指定行之后:a\

[ming@localhost test]$ sed -r '/a+/a\line1' data.txt aline1a123line1aa456line1aaa789line1b123bb456bbb789

4. 替换行: c\

[ming@localhost test]$ sed -r '/a+/c\line has a' data.txt line has aline has aline has aline has ab123bb456bbb789

5. 映射: y/source/dest/

[ming@localhost test]$ sed 'y/ab/AB/' data.txt AA123AA456AAA789B123BB456BBB789

6. 读写文件

将指定的行写入指定的文件:w File

[ming@localhost test]$ sed -nr '/a+/w a.txt' data.txt [ming@localhost test]$ cat a.txt aa123aa456aaa789

读取文件内容,并将其插到指定行之后:r File

[ming@localhost test]$ cat input.txt ----[ming@localhost test]$ sed -r '/a+/r input.txt' data.txt a----a123----aa456----aaa789----b123bb456bbb789

7. 应用变量

[ming@localhost test]$ rpl="NUMBER"[ming@localhost test]$ sed -r 's/[0-9]+/'"$rpl"'/' data.txt aaNUMBERaaNUMBERaaaNUMBERbNUMBERbbNUMBERbbbNUMBER

8. 应用脚本文件

[ming@localhost test]$ cat script.sed /a+/ {    i\a begin    a\a end}s/[0-9]+/Number/[ming@localhost test]$ sed -rf script.sed data.txt a beginaa enda beginaNumbera enda beginaaNumbera enda beginaaaNumbera endbNumberbbNumberbbbNumber