您的位置:首页 > 其它

grep的简单的用法

2016-05-05 17:35 441 查看
演示文件:

[root@cyf opt]# cat  123.txt
qqqqqqqqqq
wwwwwwwww
eeeeeeeeeaa
rrrrrr
aaaaa
111
222
333
4
5
a


1

-A表示匹配本行和之后的行

[root@cyf opt]# grep  -A 1 111 123.txt
111
222


2

-B表示匹配本行和之前的行

[root@cyf opt]# grep -B 1 111  123.txt
aaaaa
111


3

-C表示 匹配本行与上下行

[root@cyf opt]# grep  -C 1 222 123.txt
111
222
333


4

-c表示grep的参数有多少个

[root@cyf opt]# grep  -c a  123.txt
3


5

–color表示颜色显示

[root@cyf opt]# grep  --color  11 123.txt
111


6

-e后面跟正则表达式,可以跟多个正则

[root@cyf opt]# grep  -e  ^e  -e 2$  123.txt
eeeeeeeeeaa
222


7

-f指定文件内容作为查询条件

[root@cyf opt]# cat we
11
[root@cyf opt]# ls
123.txt  we
[root@cyf opt]# grep -f we 123.txt
111


8

-m 最多匹配多少个个数作为条件

[root@cyf opt]# cat  123.txt
qqqqqqqqqq
wwwwwwwww
eeeeeeeeeaa
rrrrrr
aaaaa
111
222
333
4
5
a[root@cyf opt]# grep -m 2 a 123.txt
eeeeeeeeeaa
aaaaa


9

-n表示行数计数

[root@cyf opt]# grep -n a 123.txt
3:eeeeeeeeeaa
5:aaaaa
11:a


10

-o 只打印匹配的内容

[root@cyf opt]# grep -o  11 123.txt
11
[root@cyf opt]# grep -o  1 123.txt
1
1
1


11

-R搜素子目录内容

[root@cyf opt]# grep  -R a *
1/2/3/spur:aaaaa
123.txt:eeeeeeeeeaa
123.txt:aaaaa
123.txt:a
we:a


12

-v表示查找不含参数的行

[root@cyf opt]# grep  -v a 123.txt
qqqqqqqqqq
wwwwwwwww
rrrrrr
111
222
333
4
5


13

w查找一个词

[root@cyf opt]# cat  we
a
12 33 44
55 66 77
a s d f
[root@cyf opt]# grep  -w 12 we
12 33 44
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: