Linux常用过滤命令之grep命令的使用

1.grep all 过滤出含有all三个字母的所有语句  如:
all tooall 
 to alltoall all  
allto100
uuualltoall
&&all
&all*
;all;

grep -w "all"过滤出来的是含有all这个单词的所有语句   也就是说只过滤字母数字,不过滤字符
all tooall
to alltoall all
&&all
&all*
;all;


2.grep --color=auto “root”1.txt  将root一红色标识出来
例子:cat 999.txt | grep qwer --color=auto

3.grep -v 取反参数 也就是说过滤出除了 -v后面参数的所有数据
例子:


4.grep -c "root" 1.txt    root这个文件在1.txt里出现的 行数
例子:

5.grep -n “qwer”999.txt  显示匹配的行数(在第几行出现的)


6.显示出匹配的文件 grep “qwer” 999.txt 888.txt 777.txt

7.忽略大小写字母 : grep -i “qwer” 999.txt


8.向上匹配,向下匹配,和向上向下分别匹配  (很常用!)
grep "root" -A 2   匹配含有root的那一行再多往下显示2行,最后显示三行
grep "root" -B 2   匹配含有root的那一行再多往上显示2行,最后显示三行
grep "root" -C 2   匹配含有root的那一行再多往上,往下显示2行,最后显示五行


猜你喜欢

转载自blog.csdn.net/sdb5858874/article/details/80484728