shell-grep命令
2019-08-16 本文已影响0人
哆来咪发都不会
语法
grep [选项]... PATTERN [FILE]..
-c 行数
-i 不区分大小写
-n 显示行号
-v 取反
-r 遍历所有子目录
-A 后面跟数字,过滤出符合要求的行以及下面n行
-B 同上,过滤出符合要求的行以及上面n行
-C 同上,同事过滤出符合要求的行以及上下各n行
实例
[root@localhost wang]# cat test.txt
abc
abcd
abcdef
123
1234
12345
def
abcdef
abc123
def123abc
ABC
ABCD
[root@localhost wang]# grep 'abc' test.txt
abc
abcd
abcdef
abcdef
abc123
def123abc
[root@localhost wang]# grep 'abc' -c test.txt
6
[root@localhost wang]# grep 'abc' -i test.txt
abc
abcd
abcdef
abcdef
abc123
def123abc
ABC
ABCD
[root@localhost wang]# grep 'abc' -n test.txt
1:abc
2:abcd
3:abcdef
8:abcdef
9:abc123
10:def123abc
[root@localhost wang]# grep 'abc' -nv test.txt
4:123
5:1234
6:12345
7:def
11:ABC
12:ABCD
[root@localhost wang]# grep 'abc' -A2 test.txt
abc
abcd
abcdef
123
1234
--
abcdef
abc123
def123abc
ABC
ABCD
[root@localhost wang]# grep '123' -B2 test.txt
abcd
abcdef
123
1234
12345
def
abcdef
abc123
def123abc
[root@localhost wang]# grep '123' -C2 test.txt
abcd
abcdef
123
1234
12345
def
abcdef
abc123
def123abc
ABC
ABCD
[root@localhost wang]#
过滤注释行和空行
[root@localhost wang]# cat test.txt
#abc
#abcd
#abcdef
#123
1234
12345
def
abcdef
abc123
def123abc
ABC
#ABCD
[root@localhost wang]# grep -Ev "^#|^$" test.txt
1234
12345
def
abcdef
abc123
def123abc
ABC
[root@localhost wang]#