Linux文本处理命令grep

文章目录

[隐藏]

  • 1. 查找单个关键字
  • 2. 查找多个关键字
  • 3. 多文件查找关键字
  • 4. 递归查找关键字
  • 5. 查找指定进程
  • 6. 查找进程个数
1. 查找单个关键字

举例:查找Baiduspider访问日志,并输出行号。

# grep -n Baiduspider /usr/local/nginx1.14/logs/access.log    3371:180.76.15.137 - - [31/Dec/2018:05:05:46 +0800] "GET /asset/detail/show/RG9ja2VyIENFIDE4LjA5IOS9v+eUqERvY2tlcmZpbGXlronoo4 HTTP/1.1" 200 4748 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"  4570:180.76.15.11 - - [08/Jan/2019:16:57:04 +0800] "GET /asset/detail/show/RG9ja2VyIENFIDE4LjA5IOS9v+eUqERvY2tlcmZpbGXlronoo4 HTTP/1.1" 200 7754 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"  4693:180.76.15.20 - - [09/Jan/2019:05:29:54 +0800] "GET /asset/detail/show/Q2VudE9TNy41IE15U1FMOC4wLjEzIFJQTea6kOeggee8luivke HTTP/1.1" 200 16691 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"  4924:180.76.15.160 - - [10/Jan/2019:22:30:28 +0800] "GET /asset/detail/show/SlF1ZXJ5My4zLjEg6YCJ5oup5Zmo HTTP/1.1" 200 10049 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"  ...  
2. 查找多个关键字

举例:查找Baiduspider、bingbot、Googlebot访问日志,并输出行号。

# grep -n -E 'Baiduspider|bingbot|Googlebot' /usr/local/nginx1.14/logs/access.log    5620:203.208.60.0 - - [14/Jan/2019:10:26:09 +0800] "GET / HTTP/1.1" 200 17232 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"  5639:180.76.15.21 - - [14/Jan/2019:16:39:35 +0800] "GET / HTTP/1.1" 200 17232 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"  5888:157.55.39.40 - - [17/Jan/2019:13:45:05 +0800] "GET / HTTP/1.1" 200 17232 "-" "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)"  ...  
3. 多文件查找关键字

举例:查找10.100.78.12服务器多天的监控日志。

# grep 10.100.78.12 2018-12-10.bjyz.network.monitor.log 2018-12-11.bjyz.network.monitor.log    2018-12-10.bjyz.network.monitor.log:[2018-12-10]|[13:10:00]|10.100.78.12|active  2018-12-11.bjyz.network.monitor.log:[2018-12-11]|[13:10:00]|10.100.78.12|down  
4. 递归查找关键字

举例:递归查找10.100.78.12服务器多天的监控日志。

grep -r 10.100.78.12    2018-12-10.bjyz.network.monitor.log:[2018-12-10]|[13:10:00]|10.100.78.12|active  2018-12-11.bjyz.network.monitor.log:[2018-12-11]|[13:10:00]|10.100.78.12|down  2018-12-12.bjyz.network.monitor.log:[2018-12-12]|[13:10:00]|10.100.78.12|active  
5. 查找指定进程

举例:查找java进程。

# ps -ef | grep java  
6. 查找进程个数

举例:查找java进程个数。

# ps -ef | grep -c java  

原文出处:itrfc -> http://www.itrfc.com/blog/detail/e8e50ae82dd6967fd30083d0c3573f87

本站所发布的一切资源仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如果侵犯你的利益,请发送邮箱到 [email protected],我们会很快的为您处理。
超哥软件库 » Linux文本处理命令grep