ispsh 发表于 2018-8-28 12:22:58

shell脚本三剑客之sed命令

  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/或//。
  /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 延续到下一输入行;允许跨行的模式匹配语句
  显示指定行:
  例:# 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.
  例:# sed -n '2p' data.f
  It was an evening of splendid music and company.
  释:如果不加-n,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行数据。
  显示行号:
  # 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.
  # sed -n '/floor/=' test
  3
  释:不加-n选项则显示全部和相应行号
  替换:
  # 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.
  # 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.
  # cat 123.txt
  It was an evenABCDg of splendid music and company.
  The local nurse Miss P.Neaue was ABCD attendance.
  释:只是将替换的结果存到文件中,不会将全部都放进去。
  # sed -n 's/fell/"abcd" &/p' test
  Too bad the disco floor "abcd" fell through at 23:10.
  # 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.
  # cat 456.txt
  Too bad the disco floor fell through at 23:10.
  # sed -n '14,24 w named.conf' rndc.conf
  # 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
  释:可以打印行号内容,注意这样做会覆盖原有数据。
  # 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.
  # 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.
  释:可以匹配正则内容
  # 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
  # sed '/\./q' test
  The honeysuckle band played all night long for noly $90.
  # sed '/.a.*/q' test
  The honeysuckle band played all night long for noly $90.
  释:假设要在test文档中查找“.“或“.a.*”但是要在首次匹配到就需要退出则使用q参数。
  综合联系
  # 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
  # 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
  释:第一命令是删掉虚线,第二命令是删掉空行,第三命令是删除最后一行,第四个命令是删除第一行
  去除行首:
  # 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
  # sed 's/^*//g' 123.txt
  UDP_TCP
  UDP_TCP
  UDP_TCP
  UDP_TCP
  UDP_TCP
  UDP_TCP
  UDP_TCP
  UDP_TCP
  UDP_TCP
  UDP_TCP
  添加行尾:
  # sed 's/*/& 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
  # 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传值:
  # echo $name |sed "s/the/$ABCD/g"
  Too bad ABCD disco floor fell through at 23:10\.
  # echo $name
  Too bad the disco floor fell through at 23:10\.
  # echo $ABCD
  ABCD
  释:注意sed符号要用双引号。
  从sed输出中设置shell变量:
  # echo $name
  Too bad the disco floor fell through at 23:10\.
  # echo $ABCD
  ABCD
  # NEW_NAME=`echo $name |sed "s/the/$ABCD/g"`
  # 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]
查看完整版本: shell脚本三剑客之sed命令