示例
在文件file1(file2 ...)中查找满足match_pattern(判断文件a中是否有满足match_pattern的字符串)
$grep match_pattern file1 (file2 ...)
在当前目录下查找满足 match_pattern的文件
$ grep 'match_pattern' . -R -n
./temp:public interface Expr {
./Expr.java:public interface Expr {
注:-R, -r, --recursive 等同于 --directories=recurse 递归搜索 ,把所有匹配的文件都列举出来。
-n 用于显示行号
======grep + 正则======
grep -E 'regex' file1
或者
egrep 'regex' file1
统计匹配项的数量
$ echo "abc 1 2 3 \n hello 4 5" |egrep -o '[0-9]'|wc -l
5
注:echo 作为stdin通过管道传给egrep;
-o 只输出文件中匹配到的文本部分;
wc 计量数量;