正则表达式是在搜索过程中用来匹配指定字符的字符模式。元字符在Linux中通常分为两类:Shell元字符,由LinuxShell解析;正则表达式元字符,由vi/grep/sed/awk等文本处理工具解析;正则表达式一般在文本行中执行处理,在执行下面的例子之前,先给grep命令设置--color参数:$aliasgrep='grep--color=auto'这样每次过滤后的字符串都会被着色。开始之前还有一件事要做,那就是重新创建一个测试文件,内容如下:很难回到原来的状态。正则表达式元字符特殊元字符扩展的正则表达式操作匹配所有以love$grep'^love're-filelove,howmuchIadoreyou开头的行。你知道匹配以love$grep'love$'re-fileclover结尾的所有行吗?匹配所有以l开头,中间包含两个字符,并以e$grep'l..e're-fileIhadalovelytimeonourlittlepicnic.love,howmuchIadoreyou.Doyouknowtheextentofmylove?Oh,bytheway,IthinkIlostmyglovessomewhereoutinthatfieldofclover.Didyousethem?Icanonlyhopelove.isforever.Iliveforyou.It'shardtogetbackinthe配配0个或多个空行,后面是love的字母$grep'*love're-fileIhadalovelytimeonourlittlepicnic.love,howmuchIadoreyou.Doyouknowtheextentofmylove?Oh,bytheway,IthinkIlostmyglovessomewhereoutinthatthatfieldofclover.Didyouseemthem?我只能希望爱。匹配爱或爱$grep'[Ll]ove're-file#Ihadalovelytimeonourlittlepicnic.Loverswereallaroundus.Itisspringtime.Ohlove,howmuchIadoreyou.Doyouknowtheextentofmylove?Oh,bytheway,IthinkIlostmyglovessomewhereoutinthatthatfieldofclover.Didyousethem?Icanonlyhopelove.你知道我的爱的程度吗?'[A-Z]ove're-fileLoverswereallaroundus.Itisspringtime.Oh匹配任何不在A-Z范围内的字符,全部为小写字符,顺便说一句,我想我在三叶草的那个地方丢了我的手套。你看到他们了吗?我只能希望爱是永远的。我为你而活。很难回到最佳状态。*'re-file我在我们的小野餐上度过了美好的时光。情人就在我们身边。现在是春天。哦,亲爱的,我多么喜欢你。你知道我的爱的程度吗?r.Iliveforyou.It'shardtogetbackinthegroove.我为你而活。重复前面的o字符2到4次$grep'o{2,4}'re-filegroove.至少重复o字符2次$grep'o{2,}'re-filegroove.Repeat0字符最多2次$grep'o{,2}'re-fileIhadalovelytimeonourlittlepicnic.Loverswereallaroundus.Itisspringtime.Ohlove,howmuchIadoreyou.Doyouknowtheextentofmylove?Oh,bytheway,IthinkIlostmyglovessomewhereoutinthatfieldofclover.Didyouseethem?Icanonlyhopelove.isforever.Iliveforyou.It'shardtogetbackinthegroove.重复前一个一个或一个以$egrep"go+d"linux.txtLinuxisagoodgodassdxwbcvnbvbjkgoooodfsawrerdxxhklgood0或一个字符开头的字符ansheng@Ubuntu:/tmp$egrep"go?d"linux.txtgodassdxwbcvnbvbjkgdsystemawxxxx或者,查找多个字符串$egrep".gd|good"linuxtxtLinuxisagoodgdsystemawxxxxgood组过滤器匹配$egrep"g(la|oo)d"linux.txtLinuxisagoodgladgood
