普通用法
#查看包含hello的行,取分大小写
grep 'hello' file
#查看包含hello的行,忽略大小写
grep -i 'hello' file
#查看包含hello的行,有多少行
grep -c 'hello' file
#查看包含hello的行,并且标记行号
grep -n 'hello' file
#查看不包含linx的行,v表示取反
grep -v 'hello' file
正则表达式的用法
#查找hello开头的行, -E表示是增强型的匹配,支持正则表达式
grep -En '^hello' file
#查找hello结尾的行
grep -En 'hello$' file
#匹配空行,开头和结尾没有任何东西
grep -En '^$' file
#.匹配包含一个字符的行,结果空行不包含
grep -En '.' file
#*匹配0个或者多个字符的行,结果空行也包含
grep -En '.*' file
#+代表多个字符,至少为1,查找hello不在开头并且不在结尾的行
grep -En '.+hello+.' file
#匹配只有一个字符的行
grep -En '^.$' file
#匹配包含数字的行
grep -En '[0-9]+' file
#匹配包含ip地址的行,\转义元字符
grep -En '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' file
#very至少出现一次的行,()代表单元
grep -En '(very)+' file
#very至少出现2次的行,{}修饰出现多少次
grep -En '(very){2}' file
#very出现2-4次的行
grep -En '(very){2,4}' file