shell 脚本——sed 用法

Linux sed 命令是使用脚本来处理一个或多个文本文件,简化对文件的操作

语法
sed [选项][脚本命令] 文件名

常用选项

  • r:使用扩展正则表达式
  • f:保存了sed指令的文件
  • i:直接对内容进行修改,不加-i时默认只是预览,不会对文件做实际修改
  • n:取消默认输出,sed默认会输出所有文本内容,使用-n参数后只显示处理过的行

脚本命令

  • a:新增,插入文本在指定行后
  • c:更改,更改匹配行的内容
  • d:删除,后面通常没有内容
  • i:插入,插入文本在指定行前
  • p:打印
  • s:替换,通常和正则一起使用

(1)删除空白行

sed -i '/^s*$/d' file
[root@ceph-234-310 home]# cat test
11111111111111111111111111

22222222222222222222222222
[root@ceph-234-310 home]# sed -i '/^s*$/d' test
[root@ceph-234-310 home]# cat test
11111111111111111111111111
22222222222222222222222222
[root@ceph-234-310 home]# 

(2)删除最后一个匹配的}

sed -i '$s/}//' file
[root@ceph-234-310 home]# cat test
{
   {a:b,c}
}
[root@ceph-234-310 home]# sed -i '$s/}//' test
[root@ceph-234-310 home]# cat test
{
   {a:b,c}

[root@ceph-234-310 home]#

(3)用逗号,替换第一个匹配的}

[root@ceph-234-310 home]# cat test
{
   {a:b,c}
}
[root@ceph-234-310 home]# sed  -i '0,/{/{s/{/,/}' test
[root@ceph-234-310 home]# cat test
,
   {a:b,c}
}
[root@ceph-234-310 home]#

(4)向文件中插入行

[root@ceph-234-310 home]# cat test
{
   {a:b,c}
}
[root@ceph-234-310 home]# sed '2ahello' test  #在第2行之后添加一行
{
   {a:b,c}
hello
}
[root@ceph-234-310 home]#

(5)在匹配字符串后添加内容

[root@ceph-234-310 home]# cat test
{
   {a:b,c}
   1234
}
[root@ceph-234-310 home]# sed '/123/ahello' test
{
   {a:b,c}
   1234
hello
}
# 如果有多行匹配,则每一行后面都插入字符串
[root@ceph-234-310 home]# cat test
{
   {a:b,c}
   1234
   1234
}
[root@ceph-234-310 home]# sed '/123/ahello' test
{
   {a:b,c}
   1234
hello
   1234
hello
}
# 如果要添加以a为首的字符串,则需要转义符号
[root@ceph-234-310 home]# sed '/123/aahello' test
{
   {a:b,c}
   1234
ahello
}

(6)在最后一行添加字符串

[root@ceph-234-310 home]# cat test
{
   {a:b,c}
   1234
   1234
}
[root@ceph-234-310 home]# sed '$ahello' test
{
   {a:b,c}
   1234
   1234
}
hello
[root@ceph-234-310 home]#

(7) 在指定行前新起一行插入字符串

[root@ceph-234-310 home]# cat test
{
   {a:b,c}
   1234
   1234
}
[root@ceph-234-310 home]# sed '2ihello' test # 在第2行前插入字符串
{
hello
   {a:b,c}
   1234
   1234
}
[root@ceph-234-310 home]#

(8)在包含指定字符串的行之前插入新的字符串 ,如果有多行包含,则每一行之前都会插入

[root@ceph-234-310 home]# cat test
{
   {a:b,c}
   1234
   1234
}
[root@ceph-234-310 home]# sed '/123/ihello' test
{
   {a:b,c}
hello
   1234
hello
   1234
}
[root@ceph-234-310 home]#

(9)删除指定行

[root@ceph-234-310 home]# cat test
{
   {a:b,c}
   1234
   1234
}
# 删除第3行
[root@ceph-234-310 home]# sed '3d' test    
{
   {a:b,c}
   1234
}

(10) 从第一行开始删除,每隔2行删除一行

[root@ceph-234-310 home]# cat test
{
   {a:b,c}
   1234
   1234
}
[root@ceph-234-310 home]# sed '1~2d' test
   {a:b,c}
   1234
[root@ceph-234-310 home]#

(11) 删除指定的1至3行

[root@ceph-234-310 home]# cat test
{
   {a:b,c}
   1234
   1234
}
[root@ceph-234-310 home]# sed '1,3d' test
   1234
}
[root@ceph-234-310 home]#

(12)替换文件内容,默认只替换每行第一个

[root@ceph-234-310 home]# cat test
{
   {a:b,c}
   1234123
   1234123
}
# 将字符串123替换为hello,如果出现多次,则替换多次
[root@ceph-234-310 home]# sed 's/123/hello/' test
{
   {a:b,c}
   hello4123
   hello4123
}
[root@ceph-234-310 home]#

(13)将文本中全部符合的字符串都进行替换

[root@ceph-234-310 home]# cat test
{
   {a:b,c}
   1234123
   1234123
}
[root@ceph-234-310 home]# sed 's/123/hello/g' test
{
   {a:b,c}
   hello4hello
   hello4hello
}
[root@ceph-234-310 home]#

(14)将每一行第2个匹配的字符串进行替换

[root@ceph-234-310 home]# cat test
{
   {a:b,c}
   1234123
   1234123
}
# 每一行第2个匹配
[root@ceph-234-310 home]# sed 's/123/hello/2' test
{
   {a:b,c}
   1234hello
   1234hello
}
[root@ceph-234-310 home]#

链接:https://blog.csdn.net/wdz306ling/article/details/80087889/

原文地址:https://www.cnblogs.com/fyusac/p/14577619.html