grep

1: 输出正确的文件名

 cat gbz.txt |while read f ; do h=20130715/$f/*billing0715231[0-4]*; grep -l 'img4.cache.netease.com' $h 2>/dev/null ; done

 

跟据频道得到文件

nohup cat gbz.txt |while read f; do cd /Data/billing/history/20130809/$f; grep  -l 'cc.test.ismartv.tv' *|while read h; do cp /Data/billing/history/20130809/$f/$h /Data/billing/history/20130809/gbz/; done; done  &

 2:去掉没用的 No such file or directory 报错

    用grep命令的-s选项

cat gbz.txt |while read f;do grep -s 'img.500.com' 20131031/$f/*billing1031121[0-4]* |awk '{print $4}'; done

 并求合:

 

cat gbz.txt |while read f;do grep -s 'img.500.com' 20131031/$f/*billing1031121[0-4]* |awk '{print $4}'; done |awk '{sum +=$1}END{print sum}'

 3:判断grep是否有输出

 

判断tgz文件中是否存在该文件,存在就打出来

 

ls |while read f; do tar tzvf $f |grep 01066813O5_25515_live2_20131205060502704_21526.log && echo $f || echo > /dev/null ; done

 

原理:相当于三目运算符

 

expression && code(true) || code(false)      #expression?code(true):code(true)

 4:文件中有多个匹配行时每行都输出文件名

grep -H apple *

 5:过滤不显示  -v

tail -f run.log |grep -v 'channel product billtype error'

 6:显示过滤信息的前|后n行(-C 前后)

tail -f run.log |grep -A 3 'get channel list size'
tail -f run.log |grep -B 3 'get channel list size'

 7:过滤多个条件

grep -E 'a|b|c' a.txt #egrep 'a|b|c' a.txt

 

你可能感兴趣的:(grep)