sed高级应用

系列文章目录



sed命令的语法

单个行地址

[address]command  

使用大括号惊醒分组使其用于同一个地址

[line-address]command

替换

替换命令语法

[address]s/pattern/replacement/flages

flags可以是:
n 1到512之间的数字,表示文本模式第n次出现的情况进行替换
g 对匹配的结果进行全局替换
p 打印替换后的内容
w 将模式空间的内容写到文件file中

#### 更改整行操作可以根据行号和模式匹配进行操作
 
[root@xixi]# cat cali.txt
cali	
hunan	
yongz               
rose
jack
 
#将第3行整行替换成字符串1000
[root@xixi]# sed '3c 1000' cali.txt
cali	
hunan	
1000
rose
jack
 
#将root所在的行  整行替换成字符串1000
[root@xixi]# sed '/root/c suda' /etc/passwd
suda
bin:x:1:1:bin:/bin:/sbin/nologin
 
 
[root@xieshan 0713]# cat ifcfg-ens33 
BOOTPROTO="none"
NAME="ens33"
DEVICE="ens33"
ONBOOT="yes"
IPADDR=192.168.2.43
PREFIX=24
GATEWAY=192.168.2.1
DNS1=114.114.114.114
[root@xixi 0713]# sed -i '4c ONBOOT="no"' ifcfg-ens33 
[root@xixi 0713]# cat ifcfg-ens33 
BOOTPROTO="none"
NAME="ens33"
DEVICE="ens33"
ONBOOT="no"
IPADDR=192.168.2.43
PREFIX=24
GATEWAY=192.168.2.1
DNS1=114.114.114.114
[root@xixi 0713]# sed -i '/^BOOTPROTO/c BOOTPOROTO="yes"' ifcfg-ens33 
[root@xixi 0713]# cat ifcfg-ens33 
BOOTPOROTO="yes"
NAME="ens33"
DEVICE="ens33"
ONBOOT="no"
IPADDR=192.168.2.43
PREFIX=24
GATEWAY=192.168.2.1
DNS1=114.114.114.114
[root@xixi 0713]# sed -i '/^ONBOOT/ s/no/yes/' ifcfg-ens33 
[root@xixi 0713]# cat ifcfg-ens33 
BOOTPOROTO="yes"
NAME="ens33"
DEVICE="ens33"
ONBOOT="yes"
IPADDR=192.168.2.43
PREFIX=24
GATEWAY=192.168.2.1
DNS1=114.114.114.114

在这里插入图片描述

[root@localhost ~]# cat aaa
.Ah Major Heading

[root@localhost ~]# sed '/^.Ah/{   //先匹配以.Ah开头的行
> s/\.Ah */\   
> \
> @A HEAD = /   //将.Ah* 替换为 @A HEAD = 
> s/"//g   //引号替换为空
> s/$/\    // $替换为空行
> /
> } ' aaa

//此处有空行
@A HEAD = Major Heading
//此处有空行

删除

D命令即是删除行 (delete lines)是对sed的输出流里的数据进行删除而不是直接作用到文件本身。
删除所有内容

# 1 删除文件所有内容。执行后无返回。
sed 'd' filename
 
# 示例 删除emp.txt里所有内容
sed 'd' emp.txt

删除空行

[root@localhost ~]# cat test 
aaa

aaa

aaa
[root@localhost ~]# sed '/^$/d' test 
aaa
aaa
aaa

删除指定开头

.sp
.bp
.nf
.fi
[root@localhost ~]# sed '/^.sp/d' test 
.bp
.nf
.fi
[root@localhost ~]# sed '/^.nf/d' test 
.sp
.bp
.fi

追加

追加(a)
插入(i)
更改(c)

第一行后面添加abc

r[root@localhost ~]# sed '1a abc' aaa
.Ah Major Heading
abc
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
first:second
one:two

在abc前面添加空格

[root@localhost ~]# ^C
[root@localhost ~]# cat aaa
.Ah Major Heading
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed '1a "    abc"' aaa   //将空格引起来
.Ah Major Heading
"    abc"
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
first:second
one:two

[root@localhost ~]# sed '1a /   abc' aaa   //转义
.Ah Major Heading
/   abc
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
first:second
one:two



[root@localhost ~]# sed '/^See/a 123' aaa   //在匹配指定行 后面添加
.Ah Major Heading
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
123
See Section 12.9
123
first:second
one:two


[root@localhost ~]# sed '/See.*[1-9][0-9]\.[0-9]/a abc' aaa   //在12.9后面添加abc
.Ah Major Heading
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
abc
first:second
one:two

插入

  • 在指定行 之前插入使用的是“ i”,而在指定行 之后插入是使用“ a”

在第一行插入xixi

[root@localhost ~]# cat aaa
.Ah Major Heading
[root@localhost ~]# cat aaa
.Ah Major Heading
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
first:second
one:two

[root@localhost ~]# sed '1i  xixi' aaa   //在第一行插入xixi
xixi
.Ah Major Heading
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
first:second
one:two

匹配ORA开头 插入xixi

[root@localhost ~]# cat aaa
.Ah Major Heading
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed '/^ORA/i xixi' aaa   //匹配ORA开头的 插入xixi (会使原本的在此的行 变成下一行)
.Ah Major Heading
xixi
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
first:second
one:two

插入二行

[root@localhost ~]# cat aaa 
.Ah Major Heading
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed '/^first/i xixi\   //插入两行
hehe' aaa
.Ah Major Heading
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
xixi
hehe
first:second
one:two

更改

将.Ah开头到第一匹配到的on 更改为abc

[root@localhost ~]# cat aaa
.Ah Major Heading
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
first:second
one:two
[root@localhost ~]# sed '/^\.Ah/,/on/c abc' aaa   //将.Ah开头到第一匹配到的on 更改为abc
abc
See Section 1.4
See Section 12.9
first:second
one:two

转换

[root@localhost ~]# cat test 
abc abc xxx yyy abc
abc abc xxx yyy abc
abc abc xxx yyy abc 

将所有abc转换为123

[root@localhost ~]# sed 'y/abc/123/' test  //将所有abc转换为123
123 123 xxx yyy 123
123 123 xxx yyy 123
123 123 xxx yyy 123 

将第一行的abc转换为123

[root@localhost ~]# sed '1y/abc/123/' test  //将第一行的abc转换为123
123 123 xxx yyy 123
abc abc xxx yyy abc
abc abc xxx yyy abc 

转换是将匹配到的单个字符转换为对应的 b没有匹配到不会进行转换

[root@localhost ~]# sed '1y/acc/123/' test   //转换是将匹配到的单个字符转换为对应的 b没有匹配到不会进行转换
1b2 1b2 xxx yyy 1b2
abc abc xxx yyy abc
abc abc xxx yyy abc 

打印

[root@localhost ~]# cat test 
.Ah "Comment"
.Ah "Substitution"
.Ah "Delete"

因为本身会显示一遍 p打印会显示一遍所以有两行

[root@localhost ~]# sed '/^\.Ah/{p}' test   //因为本身会显示一遍 p打印会显示一遍所以有两行
.Ah "Comment"
.Ah "Comment"
.Ah "Substitution"
.Ah "Substitution"
.Ah "Delete"
.Ah "Delete"

去掉引号 并将.Ah替换为xixi 并打印

[root@localhost ~]# cat test 
.Ah "Comment"
.Ah "Substitution"
.Ah "Delete"
[root@localhost ~]# sed '/^\.Ah/{p;s/"//g;s/\.Ah/xixi/}' test   //去掉引号 并将.Ah替换为xixi 并打印
.Ah "Comment"
xixi Comment
.Ah "Substitution"
xixi Substitution
.Ah "Delete"
xixi Delete

打印前显示行号

[root@localhost ~]# sed -n '/Comment/{=;p}' test   //打印前显示行号
1
.Ah "Comment"

加粗样式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值