linux grep工作常用

本文主要是工作中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 更简单直接统计行数

$ 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

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

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(查找含有指定字符串的文件)

$ 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/

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值