linux 中 sed命令

发布时间:2022-06-27 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了linux 中 sed命令脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
 
 
  1. 定义: 文本编辑器-(流媒体编辑器)
    • 特点: 命令方式编辑,而不是在vim里边
  2. 格式
    • sed [参数] '处理规则' 操作对象路径
  3. 参数
    • -i :  即时编辑
    • -f :  指定sed匹配规则脚本文件
    • -r :  支持拓展正则
    • -e :允许多项编辑
    • -n :  取消默认输出
  4. 定位
    1. 数字定位
      • sed '3d' 4.txt
      • sed '2,3d' 4.txt
    2. 正则定位
      • sed '/^g/d' 2.txt
    3. 数字和正则定位
      • sed '3,/^g/d' 2.txt
    4. 正则正则定位
      • sed '/^g/,/^j/d' 2.txt
  5. 编辑模式(至少与-i参数配合使用)(使用见操作演示)
  6. 案例
    1. 将nginx.conf中的注释行全部去掉
      • sed '/^ *#/d' /etc/nginx/nginx.conf
    2. 将nginx.conf中每一行之前增加注释
      • sed 's/.*/# &/g' /etc/nginx/nginx.conf
    3. 一键修改本机的ip
      • 192.168.15.100 ---> 192.168.15.101
      • 172.16.1.100   ---> 172.16.1.101
      • sed -i 's#.100#.101#g' /etc/sysconfig/network-scripts/ifcfg-eth[01]
    4. 将/etc/passwd中的root修改成ROOT
      • sed -i 's#root#ROOT#g' /etc/passwd
---------
操作演示
---------
 
模式d 删除第几行
linux 中 sed命令linux 中 sed命令
-e 多项编辑
linux 中 sed命令linux 中 sed命令
参数-n 取消sed 处理后的显示
linux 中 sed命令linux 中 sed命令
参数p 
打印
linux 中 sed命令linux 中 sed命令
参数-i 
就地编辑
linux 中 sed命令linux 中 sed命令
模式d中
 删除文件中xxx内容
linux 中 sed命令linux 中 sed命令
参数-f 匹配文件内规则
linux 中 sed命令linux 中 sed命令
上图虚假删除验证 
    加-i 参数 真正删除
linux 中 sed命令linux 中 sed命令
模式d中
 虚假删除文件中含xxx的行
linux 中 sed命令linux 中 sed命令
模式d中
 真正删除含b的行
linux 中 sed命令linux 中 sed命令
模式d中
 单正则 删除(删除以xxx开头的行)
linux 中 sed命令linux 中 sed命令
模式d中
多方式删除 
a 数字与正则
linux 中 sed命令linux 中 sed命令
b 数字与数字
  • sed '2,3d' 4.txt
c 正则与正则
linux 中 sed命令linux 中 sed命令
编辑模式a 
在某行的下一行虚假添加内容
    真实添加 加-i 参数
linux 中 sed命令linux 中 sed命令
编辑模式c 
替换某行内容
linux 中 sed命令linux 中 sed命令
编辑模式i 
新增一行(在当前行的上一行)
linux 中 sed命令linux 中 sed命令
编辑模式r 
在当前文件内容添加到另一文件的某行后边
linux 中 sed命令linux 中 sed命令
编辑模式w 
某文件指定行内容覆盖写入到另一文件
linux 中 sed命令linux 中 sed命令
编辑模式y 
替换文本某行内容的某些字符
linux 中 sed命令linux 中 sed命令
编辑模式s 
虚假替换文本中所有行的第一个xxx字符串
    真实替换加-i参数
linux 中 sed命令linux 中 sed命令
编辑模式sg
替换文本中所有的a字符串为b字符串
linux 中 sed命令linux 中 sed命令
编辑模式sgi
s///g中忽略大小写
linux 中 sed命令linux 中 sed命令
 

脚本宝典总结

以上是脚本宝典为你收集整理的linux 中 sed命令全部内容,希望文章能够帮你解决linux 中 sed命令所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: