20130603:BRE(grep\sed)和ERE(egrep\awk)的使用的区别1、^和$不在行首或行尾时为字面意思BRE:grep 'a^c' fileERE:grep -E  'a\^c' file2、匹配出现的次数BRE:grep 'ab\{2,4\}c' fileERE:grep -E 'ab{2,4}c' 13、ERE有? +4、[root@localhost ~]# cat 1abcaccadc[root@localhost ~]# grep 'a\(b\|c\)c' 1abcacc[root@localhost ~]# grep -E 'a(b|c)c' 1abcacc补充:2013-06-051、例:同时使用插入和追加命令[root@localhost ~]# cat 1abcabc[root@localhost ~]# sed '/^a/i\1\11;/^b/a\2\22' 1111;ab222c111;ab222c2、列表"l"小写L,列出不可打印的字符,如换行符。sed -n 'l' m1.txtsed -i 's/\r$//gp' m1.txt替换换行符^M$为$3、打印行号(内容)[root@localhost upload]# cat 1abcabcabc[root@localhost upload]# sed -n '/a/{=;p}' 11a4a7a[root@localhost upload]# sed -n '/a/{=p}' 11a4a7a[root@localhost upload]# sed -n '/a/=' 11474、命令n(next)匹配模式,读入模式的下一行,对读入的这一行,执行后续的操作[root@localhost upload]# cat 1abc[root@localhost upload]# sed  '/a/{n;d}' 1ac[root@localhost upload]# sed  '/a/{n;s/^$/Z/}' 1aZbc5、读(r)和写(w)命令:注意命令后只能有一个空格!文件结尾也不可加空格!    5.1读:替换一行为多行,如果要替换的内容很多时,很有用[root@localhost upload]# cat 1abcabc[root@localhost upload]# cat 2123[root@localhost upload]# sed '/b/{r 2d}' 1a123ca123c如果像上面这样替换的内容比较少的话可以直接使用c更改命令操作:[root@localhost upload]# sed '/b/{c1\> 2\> 3> }' 1a123ca123c    5.2写命令:把模式空间中匹配的行写入文件,例:分组销售人员名单[root@localhost upload]# cat 1aaa jinanbbb qingdaoccc jinanddd qingdaoeee jining[root@localhost upload]# cat 1.sed/jinan/{s///w jinan.txt}/qingdao/{s///w qingdao.txt}/jining/{s///w jining.txt}[root@localhost upload]# sed -nf 1.sed 1[root@localhost upload]# lltotal 40-rwxrwxrwx 1 root root 55 Jun  5 10:56 1-rw-r--r-- 1 root root 90 Jun  5 11:02 1.sed-rw-r--r-- 1 root root 10 Jun  5 11:05 jinan.txt-rw-r--r-- 1 root root  5 Jun  5 11:05 jining.txt-rw-r--r-- 1 root root 10 Jun  5 11:05 qingdao.txt[root@localhost upload]# cat jinan.txtaaaccc[root@localhost upload]# cat jining.txteee[root@localhost upload]# cat qingdao.txtbbbddd6、多行模式空间:命令“N”(Next)不同于next处理完N加入进来的两行,读取新行。即使N处理的指定行还有模式可以匹配。[root@localhost upload]# cat 111111111111 ab bc cd 1x22222 ab3333333 abbc cd 444445555 ab bccd ab bc cd 666 abbc cd 778 ab bc cd[root@localhost upload]# cat 1.seds/ab bc cd/X Y Z/g/ab/ {N/ab *\n* *bc *\n* *cd/ {    s/ *\n/ /    s/ab bc cd */X Y ZP\n/    }PD}[root@localhost upload]# sed -f 1.sed 111111111111 X Y Z 1x22222 ab3333333 X Y ZP444445555 X Y ZPX Y Z 666 X Y ZP778 X Y Z注:之所以替换为ZP是为了理解哪些语句影响了哪些行。

------------------------------------------------------------------------------

补充:

sed替换时忽略前N个匹配的需要替换的字符

sed 's/a/A/5g' test.txt 从第5个a开始替换