sed意为流编辑器(Stream Editor),在Shell脚本和Makefile中做为过滤器使用很是广泛,也就是把前一个程序的输出引入sed的输入,通过一系列编辑命令转换为另外一种格式输出。sed和vi都源于早期UNIX的ed工具,因此不少sed命令和vi的末行命令是相同的。html
sed命令行的基本格式为:正则表达式
sed option 'script' file1 file2 ... sed option -f scriptfile file1 file2 ...
选项含义:centos
--version 显示sed版本。 --help 显示帮助文档。 -n,--quiet,--silent 静默输出,默认状况下,sed程序在全部的脚本指令执行完毕后,将自动打印模式空间中的内容,这些选项能够屏蔽自动打印。 -e script 容许多个脚本指令被执行。 -f script-file, --file=script-file 从文件中读取脚本指令,对编写自动脚本程序来讲很棒! -i,--in-place 直接修改源文件,通过脚本指令处理后的内容将被输出至源文件(源文件被修改)慎用! -l N, --line-length=N 该选项指定l指令能够输出的行长度,l指令用于输出非打印字符。 --posix 禁用GNU sed扩展功能。 -r, --regexp-extended 在脚本指令中使用扩展正则表达式 -s, --separate 默认状况下,sed将把命令行指定的多个文件名做为一个长的连续的输入流。而GNU sed则容许把他们看成单独的文件,这样如正则表达式则不进行跨文件匹配。 -u, --unbuffered 最低限度的缓存输入与输出。
以上仅是sed程序自己的选项功能说明,至于具体的脚本指令(即对文件内容作的操做)后面咱们会详细描述,这里就简单介绍几个脚本指令操做做为sed程序的例子。缓存
a,append 追加 i,insert 插入 d,delete 删除 s,substitution 替换
如:在输出testfile内容的第二行后添加"mmzs":app
[root@VM_0_5_centos test]# cat testfile mmzs mmzsblog mmzsit [root@VM_0_5_centos test]# sed "2a mmzs" ./testfile mmzs mmzsblog mmzs mmzsit //删除2-5行后,输出删除后的结果 [root@VM_0_5_centos test]# sed "2,5d" testfile mmzs
sed处理的文件既能够由标准输入重定向获得,也能够当命令行参数传入,命令行参数能够一次传入多个文件,sed会依次处理。编辑器
sed的编辑命令能够直接当命令行参数传入,也能够写成一个脚本文件而后用-f参数指定,编辑命令的格式为:函数
/pattern/action 注:其中pattern是正则表达式,action是编辑操做
sed程序一行一行读出待处理文件,若是某一行与pattern匹配,则执行相应的action,若是一条命令没有pattern而只有action,这个action将做用于待处理文件的每一行。工具
/pattern/p 打印匹配pattern的行
/pattern/d 删除匹配pattern的行
/pattern/s/pattern1/pattern2/ 查找符合pattern的行,将该行第一个匹配pattern1的字符串替换为pattern2
/pattern/s/pattern1/pattern2/g 查找符合pattern的行,将该行全部匹配pattern1的字符串替换为pattern2
使用p命令须要注意,sed是把待处理文件的内容连同处理结果一块儿输出到标准输出的,所以p命令表示除了把文件内容打印出来以外还额外打印一遍匹配pattern的行。例如:ui
一个文件testfile的内容是: [root@VM_0_5_centos test]# cat testfile mmzs mmzsblog mmzsit abc 123 打印其中包含abc的行 [root@VM_0_5_centos test]# sed '/abc/p' testfile mmzs mmzsblog mmzsit abc abc 123 要想只输出处理结果,应加上-n选项,这种用法至关于grep命令 [root@VM_0_5_centos test]# sed -n '/abc/p' testfile abc 使用d命令就不须要-n参数了,好比删除含有abc的行 [root@VM_0_5_centos test]# sed '/abc/d' testfile mmzs mmzsblog mmzsit 123
注意:sed命令不会修改原文件,删除命令只表示某些行不打印输出,而不是从原文件中删去。spa
使用查找替换命令时,能够把匹配pattern1的字符串复制到pattern2中,例如:
[root@VM_0_5_centos test]# sed 's/bc/-&-/' testfile mmzs mmzsblog mmzsit a-bc- 123 pattern2中的&表示原文件的当前行中与pattern1相匹配的字符串 再好比: [root@VM_0_5_centos test]# sed 's/\([a-z]\)\([a-z]\)/-\1-~\2~/' testfile -m-~m~zs -m-~m~zsblog -m-~m~zsit -a-~b~c 123
pattern2中的\1表示与pattern1的第一个()括号相匹配的内容,\2表示与pattern1的第二个()括号相匹配的内容。
注意:sed默认使用Basic正则表达式规范,若是指定了-r选项则使用Extended规范,那么()括号就没必要转义了。
[root@VM_0_5_centos test]# sed 's/yes/no/;s/mm/MM/' ./testfile 注:使用分号隔开指令。 [root@VM_0_5_centos test]# sed -e 's/yes/no/' -e 's/mm/MM/' testfile 注:使用-e选项。
若是testfile的内容是:
<html><head><title>Hello World</title></head> <body>Welcome to the world of mmzs!</body></html>
如今要去掉全部的HTML标签,使输出结果为:
Hello World Welcome to the world of mmzs!
怎么作呢?若是用下面的命令:
[root@VM_0_5_centos test]# sed 's/<.*>//g' testfile
结果是两个空行,把全部字符都过滤掉了。这是由于,正则表达式中的数量限定符会匹配尽量长的字符串,这称为贪心的(Greedy)。好比sed在处理第一行时,<.*>匹配的并非或这样的标签,而是:
<html><head><title>Hello World</title>
这样一整行,由于这一行开头是<,中间是若干个任意字符,末尾是>。那么这条命令怎么改才对呢?答案以下:
错误:获得两个空行,匹配过多 [root@VM_0_5_centos test]# sed 's/<.*>//g' testfile 限定了匹配的行 [root@VM_0_5_centos test]# sed '/Hello World/s/<[/ a-z]*>//g' testfile Hello World <body>Welcome to the world of mmzs!</body></html> 不完美示例,由于{4,5}致使可扩展性差 [root@VM_0_5_centos test]# sed -r 's/<(\/)?[a-z]{4,5}>//g' testfile Hello World Welcome to the world of mmzs! 正确示例0: [root@VM_0_5_centos test]# sed 's/<[/ a-z]*>//g' testfile Hello World Welcome to the world of mmzs! 正确示例1: [root@VM_0_5_centos test]# sed 's/<[^>]*>//g' testfile Hello World Welcome to the world of mmzs!
注:上面代码中的红色部分表示pattern1部分的匹配规则,此处的/也不表示转义,是语法中的(有点相似分隔符的意思)
sed以行为单位处理文件,awk比sed强的地方在于不只能以行为单位还能以列为单位处理文件。awk缺省的行分隔符是换行,缺省的列分隔符是连续的空格和Tab,可是行分隔符和列分隔符均可以自定义,好比/etc/passwd文件的每一行有若干个字段,字段之间以:分隔,就能够从新定义awk的列分隔符为:并以列为单位处理这个文件。awk其实是一门很复杂的脚本语言,还有像C语言同样的分支和循环结构,可是基本用法和sed相似。
awk命令行的基本形式为:
awk option 'script' file1 file2 ... awk option -f scriptfile file1 file2 ...
和sed同样,awk处理的文件既能够由标准输入重定向获得,也能够当命令行参数传入,编辑命令能够直接当命令行参数传入,也能够用-f参数指定一个脚本文件,编辑命令的格式为:
/pattern/{actions} condition{actions} 和sed相似,pattern是正则表达式,actions是一系列操做
解释:awk程序一行一行读出待处理文件,若是某一行与pattern匹配,或者知足condition条件,则执行相应的actions,若是一条awk命令只有actions部分,则actions做用于待处理文件的每一行。
好比文件testfile的内容表示某商店(产品-价格-销量):
[root@VM_0_5_centos test]# cat testfile ProductA 30 13 ProductB 76 46 ProductC 55 32
打印每一行的第二列:
[root@VM_0_5_centos test]# awk '{print $2;}' testfile 30
76
55
自动变量$一、$2分别表示第一列、第二列等,相似于Shell脚本的位置参数,而$0表示整个当前行。再好比,若是某种产品的库存量低于75则在行末标注须要定货:
[root@VM_0_5_centos test]# awk '$2<75 {printf "%s\t%s\n", $0, "REORDER";} $2>=75 {print $0;}' testfile ProductA 30 13 REORDER ProductB 76 46 ProductC 55 32 REORDER
可见awk也有和C语言很是类似的printf函数。awk命令的condition部分还能够是两个特殊的condition-BEGIN和END,对于每一个待处理文件,BEGIN后面的actions在处理整个文件以前执行一次,END后面的actions在整个文件处理完以后执行一次。
awk命令能够像C语言同样使用变量(但不须要定义变量),好比统计一个文件中的空行数:
[root@VM_0_5_centos test]# cat testfile ProductA 30 13 ProductB 76 46 ProductC 55 32 [root@VM_0_5_centos test]# awk '/^ *$/ {x=x+1;} END {print x;}' testfile 2
就像Shell的环境变量同样,有些awk变量是预约义的有特殊含义的:
awk经常使用的内建变量:
FILENAME 当前输入文件的文件名,该变量是只读的 NR 当前行的行号,该变量是只读的,R表明record NF 当前行所拥有的列数,该变量是只读的,F表明field OFS 输出格式的列分隔符,缺省是空格 FS 输入文件的列分融符,缺省是连续的空格和Tab ORS 输出格式的行分隔符,缺省是换行符 RS 输入文件的行分隔符,缺省是换行符
例如打印系统中的用户账号列表:
[root@VM_0_5_centos test]# awk 'BEGIN {FS=":"} {print $1;}' /etc/passwd root bin daemon