unix脚本总结与应用实例 - 图文(6)

2019-03-22 14:56

19027008.doc Confidentiality level 密级

[^a-z] A negative range characters. Matches any character not in the specified range. For example, '[^a-z]' matches any character not in the range 'a' through 'z'. \\b Matches a word boundary, that is, the position between a word and a space. For example, 'er\\b' matches the 'er' in \but not the 'er' in \\\B \\cx Matches a nonword boundary. 'er\\B' matches the 'er' in \but not the 'er' in \Matches the control character indicated by x. For example, \\cM matches a Control-M or carriage return character. The value of x must be in the range of A-Z or a-z. If not, c is assumed to be a literal 'c' character. \\d \\D \\f \\n \\r \\s \\S \\t \\v \\w \\W \\xn Matches a digit character. Equivalent to [0-9]. Matches a nondigit character. Equivalent to [^0-9]. Matches a form-feed character. Equivalent to \\x0c and \\cL. Matches a newline character. Equivalent to \\x0a and \\cJ. Matches a carriage return character. Equivalent to \\x0d and \\cM. Matches any white space character including space, tab, form-feed, and so on. Equivalent to [ \\f\\n\\r\\t\\v]. Matches any non-white space character. Equivalent to [^ \\f\\n\\r\\t\\v]. Matches a tab character. Equivalent to \\x09 and \\cI. Matches a vertical tab character. Equivalent to \\x0b and \\cK. Matches any word character including underscore. Equivalent to '[A-Za-z0-9_]'. Matches any nonword character. Equivalent to '[^A-Za-z0-9_]'. Matches n, where n is a hexadecimal escape value. Hexadecimal escape values must be exactly two digits long. For example, '\\x41' matches \Allows ASCII codes to be used in regular expressions. \\num Matches num, where num is a positive integer. A reference back to captured matches. For example, '(.)\\1' matches two consecutive identical characters. \\n Identifies either an octal escape value or a backreference. If \\n is preceded by at least n captured subexpressions, n is a backreference. Otherwise, n is an octal escape value if n is an octal digit (0-7). \\nm Identifies either an octal escape value or a backreference. If \\nm is preceded by at least nm captured subexpressions, Page 26 , Total 83 第26页,共83页

All rights reserved 版权所有,侵权必究

19027008.doc Confidentiality level 密级

nm is a backreference. If \\nm is preceded by at least n captures, n is a backreference followed by literal m. If neither of the preceding conditions exists, \\nm matches octal escape value nm when n and m are octal digits (0-7). \\nml \%un Matches octal escape value nml when n is an octal digit (0-3) and m and l are octal digits (0-7). Matches n, where n is a Unicode character expressed as four hexadecimal digits. For example, \? matches the copyright symbol (?). 三.sed命令

sed的正则表达式用//括住。 1.sed文本的定位方法: x x,y /pattern/ /pattern/,x x,/pattern/ x,y!

2.sed编辑命令 命令 p = a/ i/ d c/ s r w q l {} n g y

All rights reserved 版权所有,侵权必究

Page 27 , Total 83 第27页,共83页

x为一行号 表示行号范围从x到y 查询包含模式的行 在给定行号上查询包含模式的行 通过行号和模式查询匹配行 查询不包括指定行号x和y的行 /pattern/pattern/ 查询包含两种模式的行 意思 打印匹配行 显示文件行号 在定位行号后附加新文本信息 在定位行号后插入新文本信息 删除定位行 用新文本替换定位文本 使用替换模式替换相应模式 从一个文件中读文本 将文本写道一个文件 第一个模式匹配完成以后退出或立即退出 显示八进制ASCII代码等价的控制字符 在定位行执行的命令组 从另一个文件中读文本下一行,并附加到下一行 将模式2粘贴到/pattern n/ 传送字符 3.sed打印文件的第二行 sed -n ?2p‘ filename

19027008.doc Confidentiality level 密级

4.sed打印文件的第一到三行 sed -n ?1,2p‘ filename 5.sed打印匹配test的行 sed -n ?/test/p‘ filename 6.sed打印匹配$的行 sed -n ?/\\$/p‘ filename

7.sed打印最后一行:$是代表最后一行的特殊字符 sed -n ?$p‘ filename

8.sed脚本文件 #!/bin/sed –f ―/company/‖ a/ ―The suddenly it happen.‖ 将以上脚本保存为append.sed,使用chmod给予其可执行权限。使用append.sed

filename运行。脚本的执行将会在filename文件中查找company,在匹配行的后一行中附加新文本,输出到屏幕上(不改变原文件)。

10.sed上例中如果将a\\改为c\\:

则为替换,匹配行被替换为新文本。

16.sed从文件读取,r选项:

把文件内容附加到匹配行company.之后

All rights reserved 版权所有,侵权必究

Page 28 , Total 83 第28页,共83页

9.sed上例中如果将a\\改为i\\:

则为插入,在匹配行的前一行中附加新文本,输出到屏幕(不改变原文件)。

11.sed删除第一行 sed ?1d‘ filename 12.sed删除第一到第三行 sed ?1,3d‘ filename 13.sed删除最后一行 sed ?$d‘ filename 14.sed替换文本

sed ?s/night/NIGHT/‘ filename #将所有night替换为NIGHT sed ?s/night//‘ filename

#将night删除

15.sed输出到文件,w选项:

把第1到2行的内容输出到文件field中,不存在则创建。

sed ?1,2w field‘ filename

19027008.doc Confidentiality level 密级

sed ?/company./r sedex.txt‘ filename

17.sed优势:

用户在获得不同格式的、带有各种奇形怪状格式控制符的文本,需要将它编成易读的文本时,就可以使用sed流编辑程序。另外,在重新处理具有比较固定格式的文本以生成新格式的文本时,也可以使用sed流编辑程序。

18.sed常见的一行命令集 命令 ?s/\\.$//g‘ ?-e/abcd/d‘ ?s/[ ][ ]*/[ ]/g‘ ?s/^[ ][ ]*//g‘ ?s/\\.[ ][ ]*/[]/g‘ ?s/^$/d‘ ?s/^.//g‘ ?s/COL\\(…\\)//g‘ ?s/^\\///g‘ ?s/[ ]*/[ ]/g‘ ?s/^[ ]//g‘ ?s/^[ ]*//g‘ ?s/[ ]*//g‘ ?s/[ ]*/[ ]/g‘ ?s/[ ][ ][ ][ ]*/[ ]/g‘

19.sed去掉字串变量前后的空格 str1=‖ 1234 ―

str2=` echo ${str1} ` 此时str2不含有前后的空格。

如果使用sed如下:

str2=‖` echo ${str1} | sed ?s/^[ ]*//g‘ | sed ?s/[ ]*$//g‘ `‖ 如果使用awk如下:

str2=‖` echo $(str1) | awk ?{print $1}‘`‖

或:

str2=‖` echo ${str1} | sed ?s/(^\\s*) | (\\s*$)//g‘ `‖

注:使用awk和sed的缘故是可以和前一次的操作一次性完成,而不必单独使用一条语句去除空格。例如下面第二个awk的作用就是去除空格:

TmpInf1=\

20.sed去除文件count中的前后的空格

Page 29 , Total 83 第29页,共83页

意思 删除以句点为结尾行 删除包含abcd的行(疑为‘/abcd/d‘) 删除一个以上空格,用一个空格代替 删除行首空格 删除句点后跟两个或多个空格,用一个空格代替 删除空行(sh不支持d,但在ksh下支持) 删除第一个字符 删除COL以及其后的三个字母 删除开头的/ 删除所有空格并用tab代替 删除行首的一个tab 删除行首的所有tab 删除所有tab 删除所有tab并用一个空格代替 每四个空格删除并使用一个tab代替 All rights reserved 版权所有,侵权必究

19027008.doc Confidentiality level 密级

tmp=`sed 's/^ *//g' count | sed 's/ *$//g' ` 则tmp为文件内容,不含有前后的空格。

21.sed提取最后一个目录名和程序名

例如从../../etc/passwd或者/etc/passwd得到passwd 方法一:使用临时文件

#得到当前路径,输出到a文件 pwd >a

#读取a文件,过滤首字母/和尾字母/,将结果输出到b文件 sed 's/^\\///g' a | sed ?s/\\/$//g‘ >b do

#读取b文件,过滤首字符串xxxx/,将结果输出到a文件 sed 's/^[a-zA-Z0-9]*\\///g' b >a #将a文件拷贝到b文件 cp -f a b done rm –f a rm –f b

方法二:使用变量(优于文件形式) c_path=`pwd`

#过滤首字母/和尾字母/

c_path=`echo $c_path | sed 's/^\\///g' | sed ?s/\\/$//g‘ ` while [ `echo $c_path | grep -c '\\/'` -gt 0 ] do

c_path=`echo $c_path | sed 's/^[a-zA-Z0-9_.]*\\///g'` done

echo $cur_path 方法三:使用basename命令 c_path=`pwd`

c_path=`basename $c_path` 注:参数扩展见五.5

while fgrep \\/ b

22.dirname或参数扩展提取目录名

例如从$0参数中提取运行的路径:从../../etc/passwd/得到../../etc,从方法一:dirname c_path=$0

c_path=`dirname $c_path`

注:如果没有路径,则c_path得到为单字符‖.‖。所以,判断是否在当前路径执

行可以使用条件 ―-$c_path‖ = ―-.‖

/etc/passwd得到/etc

方法二(参数扩展在基本sh下不支持):

Page 30 , Total 83 第30页,共83页

All rights reserved 版权所有,侵权必究


unix脚本总结与应用实例 - 图文(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:18万吨粉状磷酸一铵项目可行性研究报告

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: