Linux 正则表达式基于(sed)

命令sed是一个管道命令,也是用来接收标准输入,也是对数据进行,替换,删除,新增,选取特定功能。

1.sed 的删除行

比如删除第一行到第三行如下:

命令:ifconfig | sed '1,3d'

ifconfig | sed '1,3d'

删除后的结果如下:

 2.sed添加行。

比如在第三行添加hello

命令:ifconfig | sed '3a hello'

ifconfig | sed '3a hello'

添加结果如下:

 3.sed的整行替换。

比如替换3~5行为hello

命令:ifconfig | sed '3,5c hello'

ifconfig | sed '3,5c hello'

运行结果如下:

4.替换匹配的字符串并且替换成指定的文字。

比如将 ifconfig 中的所有RX,和TX 替换成AA

命令:ifconfig | sed 's/[RT]X/AA/'

ifconfig | sed 's/[RT]X/AA/'

5.使用sed和grep提取ip地址。

使用命令:ifconfig查看ip地址。

ifconfig

 使用命令  ifconfig | grep 'inet'       提取所有有inet字符串的行.

 ifconfig | grep 'inet'

 使用命令:ifconfig | grep 'inet' | grep '[0-9$]'        提取以数字结尾的行。

ifconfig | grep 'inet' | grep '[0-9$]' 

ifconfig | grep 'inet' | grep '[0-9$]' | grep 'broadcast' | sed '/s *//'

 使用命令:ifconfig | grep 'inet' | grep '[0-9$]' | grep 'broadcast'        匹配其中有broadcast的一行。

ifconfig | grep 'inet' | grep '[0-9$]' | grep 'broadcast'

 使用命令:ifconfig | grep 'inet' | grep '[0-9$]' | grep 'broadcast' | sed '/s *//'        替换前方的空格。

ifconfig | grep 'inet' | grep '[0-9$]' | grep 'broadcast' | sed '/s *//' 

使用命令:ifconfig | grep 'inet' | grep '[0-9$]' | grep 'broadcast' | sed '/s *//'        替换netmask后面的字符。

ifconfig | grep 'inet' | grep '[0-9$]' | grep 'broadcast' | sed '/s *//'

 成功取得ip地址。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值