grep用法详解grep与正则表达式
首先要记住的是正则表达式与通配符不一样它们表示的含义并不相同正则表达式只是一种表示法只要工具支持这种表示法,那么该工具就可以处理正则表达式的字符串。vigrepawksed等都支持正则表达式
1基础正则表达式grep工具,以前介绍过。grepaci
v搜索内容串file
amea以文本文件方式搜索c计算找到的符合行的次数i忽略大小写
顺便输出行号v反向选择,即找没有搜索字符串的行其中搜索串可以是正则表达式
1搜索有the的行并输出行号grep
theregular_expresstxt搜索没有the的行并输出行号grep
vtheregular_expresstxt
f2利用搜索集合字符表示其中的某一个字符,例如ade表示a或d或ewoodyxiaoctmpgrep
taestregular_expresstxt8Ica
tfi
ishthetest9Ohthesouptastegood
可以用符号做内的前缀,表示除内的字符之外的字符。比如搜索oo前没有g的字符串所在的行使用goo作搜索字符串woodyxiaoctmpgrep
gooregular_expresstxt2appleismyfavoritefood3Footballgameis
otusefeeto
ly18googleisthebesttoolsforsearchkeyword19goooooogleyes
内可以用范围表示,比如az表示小写字母09表示09的数字AZ则是大写字母们。azAZ09表示所有数字与英文字符。当然也可以配合来排除字符。搜索包含数字的行woodyxiaoctmpgrep
09regular_expresstxt5Howeverthisdressisabout3183dollars
f15Youarethebestisme
uyouarethe
o1
行首与行尾字符表示行的开头,表示行的结尾不是字符,是位置)那么‘’就表示空行因为只有行首和行尾。这里与里面使用的意义不同。它表示后面的串是在行的开头。比如搜索the在开头的行woodyxiaoctmpgrep
theregular_expresstxt12thesymbolisreprese
tedasstar
搜索以小写字母开头的行woodyxiaoctmpgrep
azregular_expresstxt2appleismyfavoritefood4thisdressdoes
tfitme10motorcycleischeaptha
car12thesymbolisreprese
tedasstar18googleisthebesttoolsforsearchkeyword19goooooogleyes20gogoLetsgowoodyxiaoctmp
搜索开头不是英文字母的行
fwoodyxiaoctmpgrep
azAZregular_expresstxt1