grep command
grep command grep: Global Regular Expression Print 按行处理, 输出文件中包含搜索字符串的所有行。 grep [OPTION...] PATTERNS [FILE...] 或 grep -E '123|abc' filename // 找出文件(filename)中包含 123 或者包含 abc 的行 egrep '123|abc' filename // 用 egrep 同样可以实现 判断指定文件中是否包含指定的字符串 # 文件中包含字符串,命令正常退出 $? = 0, 不包含 $? = 1 grep "prod" /path/to/file/web.xml > /dev/null if [ $? -eq 0 ]; then echo "Found!" else echo "Not found!" fi grep xxx -A5 grep xxx -B1 grep -C 5 foo file 显示 file 文件里匹配 foo 字串那行以及上下 5 行 #regex grep ".*A.*" foo.txt grep "foo\|bar" foo.txt # 统计某个字符串出现的次数 grep -o objStr filename|wc -l # 搜索子目录, recursively grep -r 'linux' * 参数 -i, –ignore-case: 忽略大小写 -A, –after-context=NUM print NUM lines of trailing context -B <显示行数> -before-context=<显示行数> #除了显示符合样式的那一行之外,并显示该行之前的内容。 -C 显示 file 文件里匹配 foo 字串那行以及上下5行 -r, -recursive, 搜索子目录 -l, -files-with-matches, 查询多文件时只输出包含匹配字符的文件名, 不打印匹配的文件内容。 -G, -basic-regexp BRE 模式,也是默认的模式 -E, -extended-regexp ERE 模式 -h, 查询多文件时不显示文件名。 -a, –text: 强制作为文本文件处理, 报错: Binary file [some_file] matches 的时候可以用。 -w pattern files : 只匹配整个单词,而不是字符串的一部分(如匹配’magic’,而不是’magical') -q, –quiet, –silent, 不显示任何东西到 stdout -P, –perl-regexp 使用 PCREs -n, –line-number 打印行号 grep 正则 https://blog.csdn.net/yufenghyc/article/details/51078107 ...