假设查找的文件为a。
1.查找出文件中含有 test字符串
grep 'test' a
带上行号:
grep -n 'test' a
2.找出文件中不带有test字符串
grep -vn 'test' a
3.找出文件中含有test或者tast字符串
grep -n 't[ae]st' a
4.找出文件中test前不以g开头
grep -n '[^g]test' a
5.文件的test不以小写字母开头
grep -n '[^a-z]test' a
或者
grep -n '[^[:lower:]]test' a
6.test只在行首出现
grep -n '^test' regular_express.txt
注意,^符号在[]以内表示反向选择,而在[]外面则表示开头!
7.去除空白行和注释行
grep -vn '^$' a|grep -v '^#'
8. '.'代表一个字符,而'*'表示出现0次或者多次
9.{m,n}表示出现m~n次,比如找出t后跟着2到3个e的
grep -n 'te\{2,3\}' a