资源描述
单击鼠标编辑标题文的格式,单击鼠标编辑大纲正文格式,第二个大纲级,第三个大纲级,第四个大纲级,第五个大纲级,第六个大纲级,第七个大纲级,第八个大纲级,第九个大纲级,*,单击鼠标编辑标题文的格式,*,单击鼠标编辑大纲正文格式,第二个大纲级,第三个大纲级,第四个大纲级,第五个大纲级,第六个大纲级,第七个大纲级,第八个大纲级,第九个大纲级,正则表达式,正则表达式,很多不同的,unix,命令都采用一种便捷一致的方法来说明匹配模式,这种方法就是正则表达式,先学习一个命令:,grep,grep -acinv,搜索字符串,filename,-c:,计算找到搜索字符串 的次数。,-i:,忽略大小写的不同,所有大小写视为相同,-n:,输出行号,-v:,反向选择,-l,只给出包含给定模式的文件列表,而不给出文件中的匹配行,搜索特定字符串,$,grep n the express,$,grep vn the express,$,grep in the express,$,grep-l the*.c,在所有源文件中查找,the,列出包含,the,的文件名,的使用,$,grep n taest express,$,grep n oo express,$,grep n goo express,$,grep n a-zoo express,$,grep n 0-9 express,和,$,$,grep n the express,$,grep n a-z express,$,grep n a-zA-Z express,$,grep n.$express,$,grep n$express,$,grep v$express|grep v#,.,和*,$,grep n g.d express,$,grep n ooo*express,$,grep n goo*g express,$,grep n g*g express,$,grep n g.*g express,$,grep n 0-90-9*express,$,grep n o2 express,$,grep n go2,5g express,$,grep n go2,g express,字符类 描述,:alnum:,字母数字,a-z A-Z 0-9,:alpha:,字母,a-z A-Z,:blank:,空格或制表键,:cntrl:,任何控制字符,:digit:,数字,0-9,:graph:,任何可视字符(无空格),:lower:,小写,a-z,:print:,非控制字符,:punct:,标点字符,:space:,空格,:upper:,大写,A-Z,:xdigit:,十六进制数字,0-9 a-f A-F,字符类 描述,注意区别以下两个命令:,grep-n:space:are express,grep-n:space:*are express,保存匹配的字符串:,(),将捕获的正则表达式匹配的字符串存储在编号为,1,到,9,的“寄存器”中。,$,grep n o2 express,$,grep n (.)express,匹配行首的第一个字符,$,grep n (.)1 express,如果一行的头两个字符相同,就匹配他们,$,grep n (.).*1$express,匹配一行中头一个字符跟最后一个字符相同的行。,正则表达式与命令行特殊符号的区别,$,ls l*ls|grep n.*,$,ls z*ls|grep n z.*,$,ls !z*,sed,的使用,非交互式行编辑器,顺序逐行将文件读入到内存中。然后,它执行为该行指定的所有操作,并在完成请求的修改之后将该行放回到内存中,以将其转储至终端,sed,工作原理图,sed,删除功能,d,d,:,Delete pattern space.Start next cycle.,观察命令结果:,sed-e d express,删除指定行:,sed-e 1d express,地址范围:,sed-e 1,10d express,从一行开始每隔一行删一行:,sed 01d express,从第二行开始每隔,7,行删一行:,sed1,7d express,sed,删除功能,带规则表达式的地址,删除注释行:,sed-e/#/d/etc/services|more,删除空行:,sed-e/$/d express,删除第一行到空行:,sed-e 1,,,/$/d express,删除最后一行:,sed-e$d express,删除包含,apple,的行到包含,Her,的行:,sed-e/apple/,/Her/d express,sed,打印功能,p,:打印命令,注意观测下面两个命令的区别,:,sed -n 1p express,sed 1p express,练习题,打印,tot,文件中从包含,BEGIN,的行开始,并且以包含,END,的行结束的文本块:,思考:,1,如果文件中没有“,BEGIN”,会怎样?,2,如果文件中没有“,END”,会怎样?,练习题,只打印,C,源文件中的,main(),函数,练习题,删除,tot,文件中从,BEGIN,开到,END,结束的块中包含,Name,的行,sed/BEGIN/,/END/Name/d;tot,sed,的语法,sed address1,address2 options command filename,sed,功能,:,替换,s/old value/new value/,实例,:,$echo my name is zsh|sed s/zsh/shz/,多次修改,1,使用,e,选项,实例,:,$echo my name is zsh|sed-e s/is/are/-e s/zsh/shz/,$,多次修改,2,用分号来分隔命令,实例,:,$echo my name is zsh|sed s/is/are/;s/zsh/shz/,注意分号必须是紧跟斜线之后的下一个字符,全局替换,g,sed,默认处理每一行中出现的第一个选定序列,如果要处理出现的每一个项目,使用,g,实例,:,$echo my name is zsh zsh|sed s/zsh/shz/g,替换每行第,N,个匹配,$echo my name is zsh zsh|sed s/zsh/shz/1,$echo my name is zsh zsh|sed s/zsh/shz/2,sed,替换功能案例,cat myfile.html,This is what I meant.,删除,HTML,标记,也就是要得到:,This is what I meant.,sed,替换功能案例,用,sed,实现,unix2dos,命令功能,unix2dos express,cat-v express,sed-i s/.$/express,思考,怎样用,sed,命令将,unix,格式文件转换成,dos,格式?,案例,案例:老大要求我替换一个大容量文件,(,大约,6-7G),的某些信息,直接用,vim,打开手动编辑的话那速度简直是苦不堪言,于是我再次想到了,awk,。仅仅是一些替换工作就用,awk,太小题大做了,而且在我用,awk,处理的时候出了一个大问题:文件太大,,awk,无法打开,我没有对这样的异常做防御,导致后面的操作出现了一连串的问题,最终的结果就是我用了一个空文本替换了刚才那个,6-7G,的文件。幸好还有备份,汗!。正在我。,老大已经搞好了,就是用了一行,sed,:,sed-i XXX-e s/XXX/XXX/g,y,命令,y,是一个管局命令,拒绝使用后缀,flag/g,sed y/1234567890/abcdefghij/sedy,n,N,命令,n N Read/append the next line of input into the pattern space.,打印奇数行:,cat-n express|sed-n p;n,打印偶数行:,cat-n express|sed-n n;p,思考下面的功能,:,cat-n express|sed-n p;N,cat-n express|sed-n N;p,cat-n express|sed-n p;d,cat-n express|sed d;n,cat-n express|sed n;d,cat-n express|sed 1d;n;d,案例,输出匹配行的下一行,sed-n/apple/n;p express,区别:,sed-n/apple/n;p express,sed,更多功能,h,:拷贝模板块的内容到内存中的缓冲区。,H,:追加模板块的内容到内存中的缓冲区,g,:获得内存缓冲区的内容并替代当前模板块中的文本。,G,:获得内存缓冲区的内容并追加到当前模板块文本的后面,=:,出行号,x,:互换模板块中的文本和缓冲区中的文本。,案例,为文件加行号,sed=express|sed N;s/n/:/,给非空行加行号,sed/./=express|sed/./N;s/n/:/,案例,删除连续重复行,sed$!N;/(.*)n1$/!P;D chongfu,案例,用,sed,实现,tac,功能,sed-e 1!G;h;$!d express,地址限定,-,匹配,将,sample_one,文件中包含,two,行的,1,替换为,2,$sed/two/s/1/2/sample_one,地址限定,-,限制行,编辑器默认查看输入到流编辑器中的每一行,且默认在输入到流编辑器中的每一行上进行编辑,$sed,5,6,s/1/2/sample_one,地址限定,除什么外,要删除除包含单词,two,的行之外的所有行,:,$sed/two/!d sample_one,two 1,two 1,two 1,$,保存输出文件,$sed/two/s/1/2/sample_one sample_two,直接修改原文件:,-i,sed-i$d express,脚本文件,$cat sedlist,/two/s/1/2/,/three/s/1/3/,注意当调用,-f,选项时,在源文件内或命令行中不使用撇号,$sed-f sedlist sample_one,one 1,two 2,three 3,one 1,two 2,two 2,three 3,$,“-f”,选项,安静模式,-n,实例,:,$sed-n-f sedlist sample_one,$,$sed-n-f sedlist sample_one sample_two,$cat sample_two,$,-n,和,-p,的配合使用,$cat sedlist,/two/s/1/2/p,/three/s/1/3/p,$,$sed-n-f sedlist sample_one,two 2,three 3,two 2,two 2,three 3,$,Sed,功能,3:,增加和插入文本,$sed$a,This is where we stop,the test sample_one,one 1,two 1,three 1,one 1,two 1,two 1,three 1,This is where we stop,the test,$,注意,:,美元符号,($),表示文本将被添加到文件的末尾。反斜线,(),是必需的,它表示将插入一个回车符。,Sed,功能,3:,增加和插入文本,$sed 3a,This is where we stop,the test sample_one,one 1,two 1,three 1,This is where we stop,the test,one 1,two 1,two 1,three 1,$,Sed,功能,3:,增加和插入文本,$sed 3i,This is where we stop,the test sample_one,one 1,two 1,This is where we stop,the test,three 1,one 1,two 1,two 1,three 1,$,添加和插入的区别,:,添加跟在指定的行之后,而插入从指定的行开始,读写文件,$sed,/two/s/1/2/,/three/s/1/3/,1,3 w sample_three sample_one,one 1,two 2,three 3,one 1,two 2,two 2,three 3,$,$,cat,sample_three,one 1,two 2,three 3,$,修改命令,替换是对字符逐个进行,而修改功能与删除类似,它影响整行:,$sed/two/c We are no longer using two sample_one,提前退出,$sed,/two/s/1/2/,/three/s/1/3/,5q sample_one,one 1,two 2,three 3,one 1,two 2,$,$sed,/two/s/1/2/,/three/s/1/3/,/three/q sample_one,one 1,two 2,three 3,$,提前退出,思考,:,sed 110q filename,有什么作用?,
展开阅读全文