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

shell脚本三剑客之sed命令

[复制链接]

尚未签到

发表于 2018-8-28 12:22:58 | 显示全部楼层 |阅读模式
  sed:
  模版:
  The honeysuckle band played all night long for noly $90.
  It was an evening of splendid music and company.
  Too bad the disco floor fell through at 23:10.
  The local nurse Miss P.Neaue was in attendance.
  sed选项如下:
  n 不打印;sed不写编辑行到标准输出,缺省为打印所有行(编辑和未编辑) 。p命令可以用来打印编辑行。(重点)
  c 下一命令是编辑命令。使用多项编辑时加入此选项。如果只用到一条 sed命令,此选项无用,但指定它也没有关系。
  f 如果正在调用s e d脚本文件,使用此选项。此选项通知 sed一个脚本文件支持所有的 sed命令,例如:sed -f myscript.sed input_file,
  这里myscript.sed即为支持sed命令的文件。
  使用sed在文件中定位文本的方式:
  x:x为一行号,如1。
  x,y:表示行号范围从x到y,如2,5表示从第2行到第5行。
  /pattern/:查询包含模式的行。例如/disk/或/[a-z]/。
  /patern/:pattern/查询包含两个模式的行。例如/disk/disks/。
  pattern/,x:在给定行号上查询包含模式的行。如 /ribbon/,3。
  x,/pattern/:通过行号和模式查询匹配行。3./vdu/。
  x,y!:查询不包含指定行号x和y的行。1,2 !。
  sed编辑命令:
  p 打印匹配行
  = 显示文件行号
  a \ 在定位行号后附加新文本信息
  i \ 在定位行号后插入新文本信息
  d 删除定位行
  c \ 用新文本替换定位文本
  s 使用替换模式替换相应模式
  r 从另一个文件中读文本
  w 写文本到一个文件
  q 第一个模式匹配完成后推出或立即推出
  l 显示与八进制A S C I I代码等价的控制字符
  { } 在定位行执行的命令组
  n 从另一个文件中读文本下一行,并附加在下一行
  g 将模式2粘贴到/pattern n/
  y 传送字符
  n 延续到下一输入行;允许跨行的模式匹配语句
  显示指定行:
  例:[root@localhost sed]# sed '2p' data.f
  The honeysuckle band played all night long for noly $90.
  It was an evening of splendid music and company.
  It was an evening of splendid music and company.
  Too bad the disco floor fell through at 23:10.
  The local nurse Miss P.Neaue was in attendance.
  例:[root@localhost sed]# sed -n '2p' data.f
  It was an evening of splendid music and company.
  释:如果不加-n,sed就不能定义文件行数。所以打印了全部信息。
  显示范围行:
  例:[root@localhost sed]# sed -n '1,3p' data.f
  The honeysuckle band played all night long for noly $90.
  It was an evening of splendid music and company.
  Too bad the disco floor fell through at 23:10.
  释:显示1到3行数据。
  显示行号:
  [root@CMN-XN-4-3g1 yingyou]# sed '/floor/=' test
  The honeysuckle band played all night long for noly $90.
  It was an evening of splendid music and company.
  3
  Too bad the disco floor fell through at 23:10.
  The local nurse Miss P.Neaue was in attendance.
  [root@CMN-XN-4-3g1 yingyou]# sed -n '/floor/=' test
  3
  释:不加-n选项则显示全部和相应行号
  替换:
  [root@CMN-XN-4-3g1 yingyou]# sed 's/\$//g' test
  The honeysuckle band played all night long for noly 90.
  It was an evening of splendid music and company.
  Too bad the disco floor fell through at 23:10.
  The local nurse Miss P.Neaue was in attendance.
  [root@CMN-XN-4-3g1 yingyou]# sed 's/in/ABCD/w 123.txt' test
  The honeysuckle band played all night long for noly $90.
  It was an evenABCDg of splendid music and company.
  Too bad the disco floor fell through at 23:10.
  The local nurse Miss P.Neaue was ABCD attendance.
  [root@CMN-XN-4-3g1 yingyou]# cat 123.txt
  It was an evenABCDg of splendid music and company.
  The local nurse Miss P.Neaue was ABCD attendance.
  释:只是将替换的结果存到文件中,不会将全部都放进去。
  [root@CMN-XN-4-3g1 yingyou]# sed -n 's/fell/"abcd" &/p' test
  Too bad the disco floor "abcd" fell through at 23:10.
  [root@CMN-XN-4-3g1 yingyou]# sed -n 's/fell/abcd &/p' test
  Too bad the disco floor abcd fell through at 23:10.
  释:匹配模式前插入你要加入的字符。
  写入:
  root@localhost sed]# sed '/bad/ w 456.txt' test
  The honeysuckle band played all night long for noly $90.
  It was an evening of splendid music and company.
  Too bad the disco floor fell through at 23:10.
  The local nurse Miss P.Neaue was in attendance.
  [root@localhost sed]# cat 456.txt
  Too bad the disco floor fell through at 23:10.
  [root@localhost etc]# sed -n '14,24 w named.conf' rndc.conf
  [root@localhost etc]# cat named.conf
  # Use with the following in named.conf, adjusting the allow list as needed:
  # key "rndckey" {
  # algorithm hmac-md5;
  # secret "Dqx3HzhRkD9X2cyL6pnLvQ==";
  # };
  #
  # controls {
  # inet 127.0.0.1 port 953
  # allow { 127.0.0.1; } keys { "rndckey"; };
  # };
  # End of named.conf
  释:可以打印行号内容,注意这样做会覆盖原有数据。
  [root@localhost sed]# sed '1,3 w 456.txt' test
  The honeysuckle band played all night long for noly $90.
  It was an evening of splendid music and company.
  Too bad the disco floor fell through at 23:10.
  The local nurse Miss P.Neaue was in attendance.
  [root@localhost sed]# cat 456.txt
  The honeysuckle band played all night long for noly $90.
  It was an evening of splendid music and company.
  Too bad the disco floor fell through at 23:10.
  释:可以匹配正则内容
  [root@localhost sed]# sed '/23:10./r 123.txt' test
  The honeysuckle band played all night long for noly $90.
  It was an evening of splendid music and company.
  Too bad the disco floor fell through at 23:10.
  abcdefg
  The local nurse Miss P.Neaue was in attendance.
  释:用r选项正则匹配行下加入123.txt文件的内容
  模式匹配首次出现后退出sed
  [root@localhost sed]# sed '/\./q' test
  The honeysuckle band played all night long for noly $90.
  [root@localhost sed]# sed '/.a.*/q' test
  The honeysuckle band played all night long for noly $90.
  释:假设要在test文档中查找“.“或“.a.*”但是要在首次匹配到就需要退出则使用q参数。
  综合联系
  [root@localhost sed]# cat test
  The honeysuckle band played all night long for noly $90.
  It was an evening of splendid music and company.
  Too bad the disco floor fell through at 23:10.
  The local nurse Miss P.Neaue was in attendance.
  ---------------------------------------------
  fasdfds
  fsdfs
  -----------------------------------------------
  safsdfdsfsdf
  sfsdfsd
  [root@localhost sed]# sed 's/-*//g' test |sed '/^$/d' |sed '$d' |sed '1d'
  It was an evening of splendid music and company.
  Too bad the disco floor fell through at 23:10.
  The local nurse Miss P.Neaue was in attendance.
  fasdfds
  fsdfs
  safsdfdsfsdf
  释:第一命令是删掉虚线,第二命令是删掉空行,第三命令是删除最后一行,第四个命令是删除第一行
  去除行首:
  [root@localhost sed]# cat 123.txt
  121 UDP_TCP
  121 UDP_TCP
  223 UDP_TCP
  2 UDP_TCP
  4324 UDP_TCP
  32 UDP_TCP
  543 UDP_TCP
  534 UDP_TCP
  654 UDP_TCP
  654 UDP_TCP
  [root@localhost sed]# sed 's/^[0-9]*//g' 123.txt
  UDP_TCP
  UDP_TCP
  UDP_TCP
  UDP_TCP
  UDP_TCP
  UDP_TCP
  UDP_TCP
  UDP_TCP
  UDP_TCP
  UDP_TCP
  添加行尾:
  [root@localhost sed]# sed 's/[0-9]*/& UDP/w 678.txt' 456.txt
  121 UDP
  121 UDP
  223 UDP
  2 UDP
  4324 UDP
  32 UDP
  543 UDP
  534 UDP
  654 UDP
  654 UDP
  [root@localhost sed]# cat 678.txt
  121 UDP
  121 UDP
  223 UDP
  2 UDP
  4324 UDP
  32 UDP
  543 UDP
  534 UDP
  654 UDP
  654 UDP
  从shell向sed传值:
  [root@localhost sed]# echo $name |sed "s/the/$ABCD/g"
  Too bad ABCD disco floor fell through at 23:10\.
  [root@localhost sed]# echo $name
  Too bad the disco floor fell through at 23:10\.
  [root@localhost sed]# echo $ABCD
  ABCD
  释:注意sed符号要用双引号。
  从sed输出中设置shell变量:
  [root@localhost sed]# echo $name
  Too bad the disco floor fell through at 23:10\.
  [root@localhost sed]# echo $ABCD
  ABCD
  [root@localhost sed]# NEW_NAME=`echo $name |sed "s/the/$ABCD/g"`
  [root@localhost sed]# echo $NEW_NAME
  Too bad ABCD disco floor fell through at 23:10\.
  ‘s/\.$/ /g’ 删除以句点结尾行
  ‘-e/abcd/d’ 删除包含abcd的行
  ‘s/[ ][ ][ ]*/[ ]/g’ 删除一个以上空格,用一个空格代替
  ‘s/^[ ][ ] * / / g’ 删除行首空格
  ‘s/\.[ ][ ]*/[ ]/g’ 删除句点后跟两个或更多空格,代之以一个空格
  ‘/^$/d’ 删除空行
  ‘s/^./ /g’ 删除第一个字符
  ‘s/COL\( ...\)/ /g’ 删除紧跟C O L的后三个字母
  ‘s/^ \/ / /g’ 从路径中删除第一个\
  ‘s/[ ]/[ ]/ /g’ 删除所有空格并用t a b键替代
  ‘S/^[ ] / /g’ 删除行首所有t a b键
  ‘s/[ ] * / /g’ 删除所有t a b键


运维网声明 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-557726-1-1.html 上篇帖子: shell脚本三剑客之grep命令 下篇帖子: Linux Shell脚本编程--Linux特殊符号大全
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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