Warning: preg_match(): Unknown modifier '{' in /home/wwwroot/yicool.com.cn/includes/learning_seo_md.php on line 63
Warning: preg_match(): Unknown modifier '{' in /home/wwwroot/yicool.com.cn/includes/learning_seo_md.php on line 63
Warning: preg_match(): Unknown modifier '{' in /home/wwwroot/yicool.com.cn/includes/learning_seo_md.php on line 63
Warning: preg_match(): Unknown modifier '{' in /home/wwwroot/yicool.com.cn/includes/learning_seo_md.php on line 63
grep 命令详解
文本处理 · 使用正则表达式搜索文本,并把匹配的行打印出来
语法
grep [选项] 模式 [文件...]
常用选项
-i 忽略大小写-n 显示行号-r -R 递归搜索目录-v 反向选择(输出不匹配的行)-E 扩展正则(等价 egrep)-P Perl 正则-o 只输出匹配到的片段-c 只统计匹配行数-A/-B/-C N 同时输出后/前/前后 N 行--include 限定文件名
实用示例
grep -rn "ERROR" /var/log --include="*.log" 递归找错误grep -E "^(GET|POST) /api" access.log 按正则筛请求
使用提示
搜索固定字符串加 -F 更快;管道里加 --color=auto 高亮
原文摘录
grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。
参考:http://man.linuxde.net/grep
示例:
# 在多级目录中对文本递归搜索(程序员搜代码的最爱):
$ grep "class" . -R -n
# 忽略匹配样式中的字符大小写
$ echo "hello world" | grep -i "HELLO"
# 匹配多个模式:
$ grep -e "class" -e "vitural" file
# 只在目录中所有的.php和.html文件中递归搜索字符"main()"
$ grep "main()" . -r --include *.{php,html}
# 在搜索结果中排除所有README文件
$ grep "main()" . -r --exclude "README"
# 在搜索结果中排除filelist文件列表里的文件
$ grep "main()" . -r --exclude-from filelist
相关命令
egrep · sed · awk · find
相关资源:Python 100 天 · 计算机教程自学 · 返回 Linux 工具页 · 返回工具箱
内容与示例整理自 dunwu/linux-tutorial,遵循 CC BY-SA 4.0;本站保留原作者署名与许可,命令仅供学习,执行前请核对目标环境。