dickrong 发表于 2018-5-23 11:00:59

Linux sed命令使用

  sed基本用法:
  sed:stream Editor 行编辑器
  sed:模式空间
  默认不编辑源文件,仅对模式空间中的数据做处理,处理结束后,将模式空间内容打印出来
  sed'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的行。
  例如:
  # sed "1,2d" /etc/issue   ---> 表示删除第1和第2行
  Mage Education Learning Services
  http://www.magedu.com
  

  # cat /etc/issue
  CentOS release 6.6 (Final)
  Kernel \r on an \m
  

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

  p:显示符合条件的行
  例如:
  # cat ./test
  he like her
  she is a good boy
  what are you doing
  # 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,显示静默模式,不显示模式空间的
  # 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
      # sed'1,2c hi' ./linux
  hi
  end
  a \string:在指定的行后面追加新行,内容为string
  例如:
  # 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
  # sed "/he/a \welcom to linux world\nthe two line" ./test --->追加两行,
  在字符间加"\n"表示换行
  

  i \string:在指定的行前面追加新行,内容为string
  例如:
  # 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(文件路径):将指定的文件的内容,添加至符合条件的行处
  例如:
  # 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
  # sed '1,2r ./first.sh' ./test --->在第1行和第2行后都添加
  

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