Shell脚本之sed命令

sed命令
行(流)编辑器,非交互式的对文件内容进行增删改查的操作,使用者只能在命令行输入编辑命令、指定文件名,然后在屏幕上查看输出。它与文本编辑器有本质的区别:
文件编辑器:编辑对象是文件
行编辑器:编辑对象是文件中的行

语法:

sed [options] '{command}[flags]' [filename]

命令选项:
-e script:将脚本中指定的命令添加到处理输入时执行的命令中
           多条件, 一行中要有多个操作
-f script: 将文件中指定的命令添加到处理输入时执行的命令中
-n : 抑制自动输出
-i : 编辑文件内容
-i.bak : 修改时同时创建.bak备份文件
-r : 使用扩展的正则表达式
! : 取反( 跟在模式条件后与shell有所区别)

sed常用内部命令:
a  在匹配后面添加
i  在匹配前面添加
p  打印
d  删除
s  查找替换
c  更改
y  转换 

flags:
数字:表示新文本替换的模式
g: 表示用新文本替换现有文本的全部实例
p: 表示打印原始的内容
w filename : 将替换的结果写入文件

示例:

cat data.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

> sed 'a\hello world' data  # 在文件data每行后面追加hello world
# 输出
1 the quick brown fox jumps over the lazy dog.
hello world
2 the quick brown fox jumps over the lazy dog.
hello world
3 the quick brown fox jumps over the lazy dog.
hello world
4 the quick brown fox jumps over the lazy dog.
hello world
5 the quick brown fox jumps over the lazy dog.
hello world

> sed '3a\hello world' data # 在文件第三行后面追加hello world
# 输出
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
hello world
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

> sed '2,4a\hello world' data # 在文件第二至四行后面追加hello world
# 输出
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
hello world
3 the quick brown fox jumps over the lazy dog.
hello world
4 the quick brown fox jumps over the lazy dog.
hello world
5 the quick brown fox jumps over the lazy dog.

> sed '/3 the/a\hello world' data # //匹配模式, 在匹配到3 the的行后面添加hello world
# 输出
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
hello world
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

> sed 'i\hello world' data # 在每行前面添加hello world
# 输出
hello world
1 the quick brown fox jumps over the lazy dog.
hello world
2 the quick brown fox jumps over the lazy dog.
hello world
3 the quick brown fox jumps over the lazy dog.
hello world
4 the quick brown fox jumps over the lazy dog.
hello world
5 the quick brown fox jumps over the lazy dog.

> sed '2,4i\hello world' data  # 在文件第二至四行前面追加hello world
# 输出
1 the quick brown fox jumps over the lazy dog.
hello world
2 the quick brown fox jumps over the lazy dog.
hello world
3 the quick brown fox jumps over the lazy dog.
hello world
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

> sed '/3 the/i\hello world' data #  //匹配模式, 在匹配到3 the的行前面添加hello world
# 输出
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
hello world
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

> sed '3d' data # 删除第三行
# 输出
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

> sed '2,4d' data # 删除第2至4行
# 输出
1 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

> sed '/3 the/d' data # 删除匹配到的行
# 输出
2 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

# 将nginx.conf文件中带#和空行都删掉
sed -r '/^#|#|^$/d' nginx.conf

> sed 's/dog/cat/' data  # 将文件中dog换成cat
# 输出
1 the quick brown fox jumps over the lazy cat.
2 the quick brown fox jumps over the lazy cat.
3 the quick brown fox jumps over the lazy cat.
4 the quick brown fox jumps over the lazy cat.
5 the quick brown fox jumps over the lazy cat.

> sed '2,4s/dog/cat' data  # 将2至4行中的dog替换成cat
# 输出
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy cat.
3 the quick brown fox jumps over the lazy cat.
4 the quick brown fox jumps over the lazy cat.
5 the quick brown fox jumps over the lazy dog.

> sed '/3 the/s/dog/cat' data # 将匹配到3 the行的dog变换成cat
# 输出
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy cat.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

> sed 'c\hello world' data # 将每行的内容更改成hello world
# 输出
hello world
hello world
hello world
hello world
hello world

> sed '3c\hello world' data # 将第三行的内容改成hello world
# 输出
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
hello world
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

> sed '2,4c\hello world' data  # 将2至4行删除,替换成hello world
# 输出
1 the quick brown fox jumps over the lazy dog.
hello world
5 the quick brown fox jumps over the lazy dog.

> sed '/3 the/c\hello world' data
# 输出
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
hello world
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

> sed 'y/abcdefg/ABCDEFG/' data  # 一一对应转换
# 输出
1 thE quiCk Brown Fox jumps ovEr thE lAzy DoG.
2 thE quiCk Brown Fox jumps ovEr thE lAzy DoG.
3 thE quiCk Brown Fox jumps ovEr thE lAzy DoG.
4 thE quiCk Brown Fox jumps ovEr thE lAzy DoG.
5 thE quiCk Brown Fox jumps ovEr thE lAzy DoG.

> sed 'p' data  # 打印两遍
# 输出
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

> sed '3p' data # 第三行打印两遍
# 输出
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

> sed '2,4p' data # 第2至4行打印两遍
# 输出
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

> sed '/3 the/p' data # 将匹配的行打印两遍
# 输出
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

标志位示例

> cat data
1 the quick brown fox jumps over the lazy dog.dog
2 the quick brown fox jumps over the lazy dog.dog
3 the quick brown fox jumps over the lazy dog.dog
4 the quick brown fox jumps over the lazy dog.dog
5 the quick brown fox jumps over the lazy dog.dog

> sed 's/dog/cat/' data
# 输出
1 the quick brown fox jumps over the lazy cat.dog
2 the quick brown fox jumps over the lazy cat.dog
3 the quick brown fox jumps over the lazy cat.dog
4 the quick brown fox jumps over the lazy cat.dog
5 the quick brown fox jumps over the lazy cat.dog

> sed 's/dog/cat/2' data # 将文件中每行匹配到第二处的dog换成cat
# 输出
1 the quick brown fox jumps over the lazy dog.cat
2 the quick brown fox jumps over the lazy dog.cat
3 the quick brown fox jumps over the lazy dog.cat
4 the quick brown fox jumps over the lazy dog.cat
5 the quick brown fox jumps over the lazy dog.cat

> sed 's/dog/cat/g' data # 将文件中每行匹配到的所有dog替换成cat
# 输出
1 the quick brown fox jumps over the lazy cat.cat
2 the quick brown fox jumps over the lazy cat.cat
3 the quick brown fox jumps over the lazy cat.cat
4 the quick brown fox jumps over the lazy cat.cat
5 the quick brown fox jumps over the lazy cat.cat

> sed '3s/dog/cat/p' data # 将第三行匹配搭配dog的第一次换成cat, 并打印两遍
# 输出
1 the quick brown fox jumps over the lazy dog.dog
2 the quick brown fox jumps over the lazy dog.dog
3 the quick brown fox jumps over the lazy cat.dog
3 the quick brown fox jumps over the lazy cat.dog
4 the quick brown fox jumps over the lazy dog.dog
5 the quick brown fox jumps over the lazy dog.dog

> sed '3s/dog/cat/w mfile # 将修改的那行写入mfile

> sed -n '3s/dog/cat/p' data # 只打印修改过的那行
3 the quick brown fox jumps over the lazy cat.dog

> sed -e 's/brown/green/;s/dog/cat/' data  # 执行多条命令
# 输出
1 the quick green fox jumps over the lazy cat.dog
2 the quick green fox jumps over the lazy cat.dog
3 the quick green fox jumps over the lazy cat.dog
4 the quick green fox jumps over the lazy cat.dog
5 the quick green fox jumps over the lazy cat.dog

> cat myfile
s/brown/green
s/dog/cat

> sed -f myfile data  # 执行文件中的命令
# 输出
1 the quick green fox jumps over the lazy cat.dog
2 the quick green fox jumps over the lazy cat.dog
3 the quick green fox jumps over the lazy cat.dog
4 the quick green fox jumps over the lazy cat.dog
5 the quick green fox jumps over the lazy cat.dog

修改源文件:

> sed -i 's/dog/cat/g' data   # 无输出,直接修改data源文件

> sed -i.bak 's/cat/dog/g' data # 先将文件备份成data.bak, 再修改data文件
> ls
 data  data.bak

其他示例:

> echo "tom is cool" | sed 's/tom/basim/'  
# 输出
basim is cool

> sed -n '$=' data   # 统计文件行数
5

> sed '=' data  # 打印data内容时加上行号
# 输出
1
1 the quick brown fox jumps over the lazy dog.dog
2
2 the quick brown fox jumps over the lazy dog.dog
3
3 the quick brown fox jumps over the lazy dog.dog
4
4 the quick brown fox jumps over the lazy dog.dog
5
5 the quick brown fox jumps over the lazy dog.dog

> sed -n -r '/^(root)(.*)(bash)$/p' /etc/passwd  # 打印root开头, bash结尾,中间任意的
# 输出
root:x:0:0:root:/root:/bin/bash
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值