SED

两种执行方式
-n 禁止默认输出

1
2
sed [-n] [-e] 'command' file(s)
sed [-n] -f scriptfile file(s)

command结构

1
2
3
4
5
- [addr [,addr]] [!] action [arguments]
- [/pattern/[,/pattern/]] {
cmd
cmd
}

action

  • a 拼接
  • c 替换
  • i 插入
  • d 删除
  • s 替换
  • y 翻译
  • p 打印

举例

1
2
3
4
5
6
7
8
9
10
11
# only print
sed -ne '/regexp/p' inputfile(s)
sed -n '/GREEN/,$p' inputfile(s)

# append after the pattern (-i 直接修改源文件)
sed -i '/pattern/a \
line1 \
line2' inputfile

# insert before the pattern
sed -i '/This is line two/i\your text 1\nyout text 2' inputfile(s)