设为首页 收藏本站
查看: 989|回复: 0

[经验分享] Linux sed命令使用

[复制链接]

尚未签到

发表于 2018-5-23 11:00:59 | 显示全部楼层 |阅读模式
  sed基本用法:
  sed:stream Editor 行编辑器
  sed:模式空间
  默认不编辑源文件,仅对模式空间中的数据做处理,处理结束后,将模式空间内容打印出来
  sed  [options]'AddressCommand' file ...
  是对file中的文件中的符合Address指定地址范围内的行执行Command编辑命令
  options:
  -n:静默模式
  不显示模式空间中的内容,仅显示被匹配的内容
  -i:直接修改原文件
  -e SCRIPT(脚本) -e SCRIPT:可以同时执行多个脚本 每个-e就可以执行一个 脚本
  -f /path/to/sed_srcipt:将-e的脚本保存在一个文件中,通过-f读取此文件执行
  sed -f /path/to/sed_srcipt
  -r:表示使用扩展的正则表达式
  Address方式:
  1. StartLine,EndLine
  格式例如:1,100,从第1行到第100行
  $:表示最后一行
  2./Pattern(正则表达式)/
  例如:/^root/  表示以root开头的行
  3. /pattern1/,/pattern2/
  表示:第1次被pattern1匹配到的行开始,第1次被pattern2匹配到的行结束,这中间的所有行
  4.LineNumber
  指定的行
  5.StartLine,+N
  表示:从指定的StartLine行开始,向后的N行
  

  Command:
  d:删除符合条件的行
      $ sed '2d' example-----删除example文件的第二行。
  *
  $ sed '2,$d' example-----删除example文件的第二行到末尾所有行。
  *
  $ sed '$d' example-----删除example文件的最后一行。
  *
  $ sed '/test/'d example-----删除example文件所有包含test的行。
  例如:
  [root@localhost data]# sed "1,2d" /etc/issue   ---> 表示删除第1和第2行
  Mage Education Learning Services
  http://www.magedu.com
  

  [root@localhost data]# cat /etc/issue
  CentOS release 6.6 (Final)
  Kernel \r on an \m
  

  Mage Education Learning Services
  http://www.magedu.com
  [root@localhost data]# sed "1d" /etc/issue    --->表示删除第1行
  [root@localhost data]# sed "1,+3d" /etc/issue -->表示删除第1行以及往后的3行,共4行
  [root@localhost data]# sed "/^\//d" /etc/issue --->表示删除以/开头的行,模式必须要//夹起来
  

  p:显示符合条件的行
  例如:
  [root@localhost data]# cat ./test
  he like her
  she is a good boy
  what are you doing
  [root@localhost data]# sed /he/p ./test  --->将行中包含he的行显示出来
  he like her
  he like her---->从执行结果看,符合模式匹配的行显示了2次,原因是sed命令的默认意思
  she is a good boy将模式空间中的命令行处理过后还要显示出来,显示的那一行在模式空间中
  she is a good boy还存在,sed命令模式是显示模式空间,因此能匹配到的显示2次,如果不想
  what are you doing          显示模式空间中的,可以是用-n,显示静默模式,不显示模式空间的
  [root@localhost data]# sed -n /he/p ./test   --->静默模式,只显示符合条件的行
  he like her
  she is a good boy
      c:取代,c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行
      例如:
      root@localhost ruby]# cat ab
       Hello!
       ruby is me,welcome to my blog.
       end
      [root@localhost data]# sed  '1,2c hi' ./linux
  hi
  end
  a \string:在指定的行后面追加新行,内容为string
  例如:
  [root@localhost data]# sed "/he/a \welcom to linux world" ./test -->在行中有he的行后追加一行
  he like her
  welcom to linux world
  she is a good boy
  welcom to linux world
  what are you doing
  [root@localhost data]# sed "/he/a \welcom to linux world\nthe two line" ./test --->追加两行,
  在字符间加"\n"表示换行
  

  i \string:在指定的行前面追加新行,内容为string
  例如:
  [root@localhost data]# sed "/he/i \welcom to linux world" ./test
  welcom to linux world
  he like her
  welcom to linux world
  she is a good boy
  what are you doing
  r file(文件路径):将指定的文件的内容,添加至符合条件的行处
  例如:
  [root@localhost data]# sed '2r ./first.sh' ./test  --->此处必须要用单引号,双引号就出错
  he like her
  she is a good boy
  #!/bin/bash
  U=`wc -l  /etc/rc.d/rc.sysinit |cut -d' ' -f1`
  G=`wc -l /etc/rc.d/init.d/functions |cut -d' ' -f1`
  sum=$[$U+$G]
  echo $sum
  what are you doing
  [root@localhost data]# sed '1,2r ./first.sh' ./test --->在第1行和第2行后都添加
  

  w file(文件路径):将指定范围内的内容另存在指定的文件中
  例如:
  [root@localhost data]# sed '/he/w ./xx.txt' ./test -->将./test文件中符合条件的行保存至./xx.txt
  he like her文件中,事先xx.txt可以不存在,如果多次执行
  she is a good boy保存在xx.txt中是覆盖追加的,此处还显示在屏幕
  what are you doing 上是因为,工作在模式空间而不是静默模式
  [root@localhost data]# cat ./xx.txt
  he like her
  she is a good boy
  s/pattern/string/修饰符:将每行中符合pattern模式的行替换为string,默认只替换每行中第一次被模式匹配到的字符串
  或s@@@修饰符或s###修饰符都行,string是字符串,不能是正则表达式
  如果要全部替换需要加修饰符
  g:全局替换
  i:查找时忽略大小写
  &:引用模式匹配整个串
  例如:
  [root@localhost data]# sed 's/o/T/' ./test --->将查找的每行中第1个"o"的替换为T
  he like her
  she is a gTod boy
  what are yTu doing
  [root@localhost data]# sed 's/o/@/g' ./test -->加上g修饰符,则全部替换
  he like her
  she is a g@@d b@y
  what are y@u d@ing
  [root@localhost data]# sed 's@\(l..e\)@\1r@g' ./test
  he liker her
  she is a good boy
  what are you doing
  [root@localhost data]# sed 's@l..e@&r@g' ./test --->此处@代表之前的整个模式l..e,\1和&在不同的地方有不同的方便
  he liker her
  she is a good boy
  what are you doing
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-480285-1-1.html 上篇帖子: Linux perror(转) 下篇帖子: Linux join命令
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表