免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1155 | 回复: 0
打印 上一主题 下一主题

linux下的sed command [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-08-18 09:38 |只看该作者 |倒序浏览

                                                               
               
                sed:
syntax: sed [options] 'command' [files]
       sed [options] -e 'conmand1' -e 'command2' ......[files]
       sed [options] -f script [files]
每次读取一行
frequently options
  -e cmd[command abbreviate]
          the next argument is a command.this is not need for a single command, but is required for all commands when multiple commands was specified
  -f file        the next argument is a script
  -g        treat all substitution as global
  frequence used parameter
  d     delete lines
  y    translate characters. similar to the tr command.(针对每一个字符)
  s    make stitution. the syntax is s/pattern/replacement/[flags](把pattern看作一个整体)
  the following flags can be specified for the s command:
  g   replace all instance of pattern, not just the first
  n   replace nth instance of pattern, the default is 1
  p   print the line if a successful substitution is done. generally used with the -n command-line option
  w file            print the line to file if a successful is done
  
for example:
  delete lines 3 through 5 of file
  sed '3,5d' file1
  delete the lines of file1 that contain a # at the begin of the line:
  $sed '/^#/d' file1
  delete any line that don't contain #keepme:
  sed '/#keepme/!d' file1
  delete lines containing only whitespace(spaces or tabs). in the
example, tab means the single tab character and is precede by a single
space
  sed '/^[ tab]*$/d' file1
  subtitute a single space for any number of spaces wherever they occur on the line:
  sed 's/ */ /g' file1
  substitue def for abc from line 11 to 20, wherever it occurs on the lines:(是将字符串abc替换,而不是每一个字母)
  sed '11,20s/abc/def/g' file1
  translate the characters a, b, c to the @ character from line 11 to 20, wherever they occur on the line:
  sed '11,20y/abc/@@@/'
  translate characters   sed 'y/abc/xyz/' file1   every instance of a is replaced to x, b to y, c to z.
  write the @ symbol for all empty line in file1
  sed '/^$/@/g' file1
  将所有的小数替换成&*,和appel替换成apple
  sed -e s/[0-9]\.[0-9][0-9]/&*/g -e s/appel/apple/g
  substitution all after brucket to blank(替换所有以'('开头的为空)

$id|sed 's/(.*/ /'
  use the command from the external file sedmds.replace the third and fourth double quotation marks with ( and ) on lines 1 through 10 in file1.make no change from 11 to the end of the file.script file sedmds contains:
  1,10{
  s/"/(/3
  s/"/)/4}
  the command is execute using the -f option
  sed -f sedcmds file1
  in the example, since the third double quote has been replace with (, it is no counted as a double quote by the second command. thus, the second command will operate on the fifth double quote character in the original file1. if the input line starts out with:" " " " "  and the stitution consequence is " " ( " )
  when stitution characters is error. above instance is ok.
假如文件files里含有如下的内容:
pintf "please input a file"
printf "I hava inputed file"
执行如下的命令
$sed 's/i/@/2/'  files
输出结果如下:
pintf "please @nput a file"
printf "I hava @nputed file"
  一些有用的正则表达式
  /^$/    空白行
  /^.*$/   一整行
  /*/     一个或多个空格
  /]*>/  HTML(或XML)标记符
  /[a-zA-z][a-zA-Z]*:[0-9a-zA-Z]*.*/  有效的URL
  /\$[0-9]*\.[0-9][0-9]/          格式化的美元数
               
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/45689/showart_362648.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP