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

shell笔记之sed编辑器的基础用法(中)

[复制链接]

尚未签到

发表于 2018-8-18 08:51:57 | 显示全部楼层 |阅读模式
  接上篇,sed替换命令有四种替换标记
  


  • 数字:表示新文本替换的模式
  


  • g:表示用新文本替换现有文本的全部实例
  


  • p:表示打印原始行的内容

  


  • w file:将替换的结果写入文件
  

  在第一种替换标记中,使用数字,可以指定替换文本行中字符出现的准确位置,例子如下:
  


  • $ sed 's/test/study/2' test2.txt
  • this is script test, we are study bash shell
  • this is script test, we are study bash shell
  • this is script test, we are study bash shell
  • this is script test, we are study bash shell
  • this is script test, we are study bash shell
  

  此例子,是直接指定替换每行里面第二次出现的匹配字符,第一个匹配字符是不被替换的。这就是数字替换标记的妙用。
  g替换标记能够替换文本模式中每一个出现的匹配字符。
  


  • $ sed 's/test/study/g' test2.txt
  • this is script study, we are study bash shell
  • this is script study, we are study bash shell
  • this is script study, we are study bash shell
  • this is script study, we are study bash shell
  • this is script study, we are study bash shell
  

  与上述数字替换标记相比,g将所有出现的匹配字符全部替换为需要的新字符了。
  p替换标记会打印包含替换命令中匹配模式的那一行,经常和-n选项一起使用。
  示例如下:
  


  • $ cat test2.txt
  • this is script test, we are test bash shell
  • this is script test, we are test bash shell
  • this is script test, we are test bash shell
  • this is script test, we are test bash shell
  • this is script test, we are test bash shell
  


  • $ sed -n 's/test/study/p' test2.txt
  • this is script study, we are test bash shell
  • this is script study, we are test bash shell
  • this is script study, we are test bash shell
  • this is script study, we are test bash shell
  • this is script study, we are test bash shell
  

  只有加-p的时候,才能打印匹配行,不加-p,执行命令后,不会有任何显示。
  w替换标记生成相同的输出,但是会将输出保存到制定的文件中:
  


  • $ sed 's/test/study/w test3' test2.txt
  • this is script study, we are test bash shell
  • this is script study, we are test bash shell
  • this is script study, we are test bash shell
  • this is script study, we are test bash shell
  • this is script study, we are test bash shell
  


  • $ cat test3
  • this is script study, we are test bash shell
  • this is script study, we are test bash shell
  • this is script study, we are test bash shell
  • this is script study, we are test bash shell
  • this is script study, we are test bash shell
  

  看到了吧,加上w参数后,直接将替换输出的文本内容保存到新建的test3文本中了,很实用的替换标记。需要注意的是,w参数只将被替换的文本行输出到要保存的文本当中。
  在文本字符串当中,我们还会遇到不容易在替换模式中实用的字符,在linux环境下最常见的就是正斜杠了。替换某个文件中的路径名,会觉得很困难。举个例子,要用cshell替换/etc/passwd文件中的bash shell,必须按照下面这样做:
  


  • $ sed 's/\/bin\/bash/\/bin\/csh/' /etc/passwd/
  

  正斜杠被用作字符串定界符,若正斜杠出现在模式文本中,必须使用反斜杠使其转义,否则会导致错误和混淆。
  要解决这个问题,也有一个办法,sed编辑器允许替换命令中的字符串定界符选择一个不同的字符:
  


  • $ sed 's!/bin/bash!/bin/csh!' /etc/passwd
  

  默认情况下sed编辑器使用的命令应用于文本中所有的数据行,如果我们只想改变文本但中的某一行或者一组数据行的话,那就要使用到行寻址。在sed编辑器当中,有两种行寻址的形式。
  


  • 行的数值范围
  


  • 筛选行的文本模式
  

  两种形式使用相同格式的指定地址:
  


  • [address]command
  

  同时,也能将多个命令组合在一起,应用到特定地址上:
  


  • address {
  •     command1
  •     command2
  •     command3
  • }
  

  sed编辑器将指定的每一个命令仅用于指定地址匹配的行
  第一种:数字寻址法
  所谓数字寻址法,就是利用数字表示行号,利用sed编辑器可以修改掉指定的数字行号。在数字寻址当中,sed编辑器默认将文本中的第一行定义为数字1,接下来的行号延续下去。在命令中指定的地址可以是单个行号,也可以由起始行号、逗号和结束行号来指定一个寻址范围。看下面这个例子:
  


  • $ sed '2s/study/test/' test2.txt
  • this is script study, we are test bash shell
  • this is script test, we are test bash shell
  • this is script study, we are test bash shell
  • this is script study, we are test bash shell
  • this is script study, we are test bash shell
  

  这个例子很明显,我们将文本中第二行利用数字2 寻址,将study替换为test了。
  继续看下面这个例子,我们使用文本行数字范围进行寻址替换:
  


  • $ sed '2,3s/study/test/' test2.txt
  • this is script study, we are test bash shell
  • this is script test, we are test bash shell
  • this is script test, we are test bash shell
  • this is script study, we are test bash shell
  • this is script study, we are test bash shell
  

  指定数字范围后,将指定的2,3两行的字符串替换为需要的字符串了,真的很方便。
  如果要将命令应用于文本行内某一点开始直到文本结束的一组文本行,那就需要用到特殊符号—美元符号$
  


  • $ sed '2,$s/study/test/' test2.txt
  • this is script study, we are test bash shell
  • this is script test, we are test bash shell
  • this is script test, we are test bash shell
  • this is script test, we are test bash shell
  • this is script test, we are test bash shell
  

  $在这里发挥了最佳的数字寻址作用!
  第二种:使用文本模式筛选器
  文本模式筛选器稍微有些复杂,命令格式为:
  


  • /pattern/command
  

  在使用文本筛选器的过程中,必须要使用正斜杠标记指定的pattern。sed编辑器只将该命令应用于指定的文本模式行。看下面这个例子,如果我们只想更改用户renyudi2默认的shell,可以使用如下方式:
  


  • $ sed '/renyudi2/s/bash/csh/' /etc/passwd
  • renyudi2:x:501:501::/home/renyudi2:/bin/csh
  • mysql:x:502:502::/home/mysql:/bin/bash
  • extmail:x:503:503::/home/extmail:/bin/bash
  • postfix:x:504:504::/home/postfix:/bin/bash
  • vmta:x:5000:5000::/var/vmta:/bin/bash
  • sysadmin:x:5001:5001::/home/sysadmin:/bin/bash
  

  sed编辑器有时候在处理文本字符多处的时候,就要用到组合命令了。组合命令需要将所用要执行的命令用大括号扩起来。这个时候,sed编辑器将处理地址行上列出的所有命令。实例如下:
  


  • $ sed '2{
  • > s/script/study/
  • > s/test/learn/g
  • > }' test2.txt
  • this is script test, we are test bash shell
  • this is study learn, we are learn bash shell
  • this is script test, we are test bash shell
  • this is script test, we are test bash shell
  • this is script test, we are test bash shell
  

  多个命令组合的效果相当不错哦,上述例子指定替换了第二行的两个字符,而且在替换/test/learn/的时候还加了g,替换了行中出现的全部匹配字符。组合命令超强吧!本节就先记录到此,下篇继续!



运维网声明 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.iyunv.com/thread-553275-1-1.html 上篇帖子: Linux-Shell脚本学习心得(第二天) 下篇帖子: SHELL -- echo的问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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