前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >linux grep工作常用

linux grep工作常用

作者头像
bear_fish
发布2018-09-14 10:07:38
5.8K0
发布2018-09-14 10:07:38
举报
文章被收录于专栏:用户2442861的专栏

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1338392

本文主要是工作中grep的常见使用:

  1. grep日志统计(单个多个文件)计数
  2. grep and or not
  3. grep 多个文件匹配查找
  4. grep -A -B 关键匹配前后几行的重要信息

grep -c ‘text’ filename(log指定text行数统计)


工作中我们往往要统计日志中某些字符串(一行行)的统计信息,

一般 grep ‘text’ filename | wc -l

其实: grep -c ‘text’ filename 更简单直接统计行数

代码语言:javascript
复制
$ grep -c "go" demo_text
6

When you want do find out how many lines that does not match the pattern
$ grep -v -c this demo_file
4    

grep or 查询


  1. grep ‘pattern1|pattern2’ filename
  2. grep -E ‘pattern1|pattern2’ filename
  3. egrep ‘pattern1|pattern2’ filename
  4. grep -e pattern1 -e pattern2 filename

例如统计文件数量的时候(往往第一行要减掉)

代码语言:javascript
复制
root@ubuntu:/data6/light/images/others# grep -c -e "JPEG" -e"jpg" <(ll)
16581
root@ubuntu:/data6/light/images/others# grep -c "" <(ll)
16582

grep and 查询


  1. grep -E ‘pattern1.*pattern2’ filename
  2. grep -E ‘pattern1.*pattern2|pattern2.*pattern1’ filename

grep not 查询

grep -v

grep practice(-A -B..)


比如在查看caffe训练的时候,看accuracy同时看前后两行的loss信息以及learning rate

grep -inr “Text” folder/to/be/searched/搜索当前目录下所有含有”Text”的文件

The r stands for recursive and so will search in the path specified and also its sub-directories. (循环递归当前目录以及子目录)

i stands for ignore case (optional in your case).(忽略大小写)

-n is line number

If your grep doesn’t support recursive search, you can combine find with xargs:

find / -type f | xargs grep ‘text-to-find-here’

当前目录下查找txt文件

find . -name “*.txt” | xargs grep -i “text_pattern”

Display only the file names which matches the given pattern using grep -l(查找含有指定字符串的文件)

代码语言:javascript
复制
$ grep -l this demo_*
demo_file
demo_file1

Searching in all files recursively using grep -r(查找多有文件,这样方便统计日志)

$ grep -r “error_msg” *

linux命令的很多技巧基本google到的,很多参考下面这个bolg

http://www.thegeekstuff.com/category/sed/

关键词英文对了,很多问题瞬间解决。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年08月31日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • grep -c ‘text’ filename(log指定text行数统计)
  • grep or 查询
  • grep and 查询
  • grep not 查询
  • grep practice(-A -B..)
  • grep -inr “Text” folder/to/be/searched/搜索当前目录下所有含有”Text”的文件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档