Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来。grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户。

grep的工作方式是这样的,它在一个或多个文件中搜索字符串模板。如果模板包括空格,则必须被引用,模板后的所有字符串被看作文件名。搜索的结果被送到标准输出,不影响原文件内容。

grep可用于shell脚本,因为grep通过返回一个状态值来说明搜索的状态,如果模板搜索成功,则返回0,如果搜索不成功,则返回1,如果搜索的文件不存在,则返回2。我们利用这些返回值就可进行一些自动化的文本处理工作。

一.命令格式:

grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数>][-d<进行动作>][-e<范本样式>][-f<范本文件>][--help][范本样式][文件或目录...]

二.命令功能:

用于过滤/搜索的特定字符。可使用正则表达式能多种命令配合使用,使用上十分灵活。

三.命令参数:

[root@localhost autoAweme]# ps -ef|grep uwsgi
root      30568    795  0 12月01 ?      00:00:23 /home/hc/project/envs/pgc/bin/uwsgi --ini /home/hc/project/pgc.ini
root      30578  30568  0 12月01 ?      00:00:00 /home/hc/project/envs/pgc/bin/uwsgi --ini /home/hc/project/pgc.ini
root      59379    795  1 12月04 ?      00:13:13 /home/hc/project/envs/autoAweme/bin/uwsgi --ini /home/hc/project/autoAweme.ini
root      59406  59379  0 12月04 ?      00:00:03 /home/hc/project/envs/autoAweme/bin/uwsgi --ini /home/hc/project/autoAweme.ini
root     112483  86053  0 09:29 pts/1    00:00:00 grep --color=auto uwsgi

除最后一条记录外,其他的都是查找出的进程;最后一条记录结果是grep进程本身,并非真正要找的进程。

2. 查找指定进程时,不显示grep 本身进程

ps aux | grep uwsgi | grep -v "grep"
ps aux|grep [u]wsgi 
ps aux|grep /[u]wsgi 
[root@localhost test]# ps -aux | grep uwsgi 
root      30568  0.0  1.3 310244 53696 ?        S    12月01   0:23 /home/hc/project/envs/pgc/bin/uwsgi --ini /home/hc/project/pgc.ini
root      30578  0.0  1.2 310244 49556 ?        S    12月01   0:00 /home/hc/project/envs/pgc/bin/uwsgi --ini /home/hc/project/pgc.ini
root      59379  1.5  1.3 457952 50236 ?        Sl   12月04  13:50 /home/hc/project/envs/autoAweme/bin/uwsgi --ini /home/hc/project/autoAweme.ini
root      59406  0.0  1.3 467760 53104 ?        S    12月04   0:03 /home/hc/project/envs/autoAweme/bin/uwsgi --ini /home/hc/project/autoAweme.ini
root      87765  0.0  0.0 112704  1016 pts/1    R+   10:14   0:00 grep --color=auto uwsgi
[root@localhost test]# ps -aux | grep uwsgi | grep -v "grep"
root      30568  0.0  1.3 310244 53696 ?        S    12月01   0:23 /home/hc/project/envs/pgc/bin/uwsgi --ini /home/hc/project/pgc.ini
root      30578  0.0  1.2 310244 49556 ?        S    12月01   0:00 /home/hc/project/envs/pgc/bin/uwsgi --ini /home/hc/project/pgc.ini
root      59379  1.5  1.3 457952 50236 ?        Sl   12月04  13:50 /home/hc/project/envs/autoAweme/bin/uwsgi --ini /home/hc/project/autoAweme.ini
root      59406  0.0  1.3 467760 53104 ?        S    12月04   0:03 /home/hc/project/envs/autoAweme/bin/uwsgi --ini /home/hc/project/autoAweme.ini
[root@localhost test]# ps -aux | grep [u]wsgi
root      30568  0.0  1.3 310244 53696 ?        S    12月01   0:23 /home/hc/project/envs/pgc/bin/uwsgi --ini /home/hc/project/pgc.ini
root      30578  0.0  1.2 310244 49556 ?        S    12月01   0:00 /home/hc/project/envs/pgc/bin/uwsgi --ini /home/hc/project/pgc.ini
root      59379  1.5  1.3 457952 50236 ?        Sl   12月04  13:45 /home/hc/project/envs/autoAweme/bin/uwsgi --ini /home/hc/project/autoAweme.ini
root      59406  0.0  1.3 467760 53104 ?        S    12月04   0:03 /home/hc/project/envs/autoAweme/bin/uwsgi --ini /home/hc/project/autoAweme.ini
[root@localhost test]# ps -aux | grep \[u]wsgi
root      30568  0.0  1.3 310244 53696 ?        S    12月01   0:23 /home/hc/project/envs/pgc/bin/uwsgi --ini /home/hc/project/pgc.ini
root      30578  0.0  1.2 310244 49556 ?        S    12月01   0:00 /home/hc/project/envs/pgc/bin/uwsgi --ini /home/hc/project/pgc.ini
root      59379  1.5  1.3 457952 50236 ?        Sl   12月04  13:45 /home/hc/project/envs/autoAweme/bin/uwsgi --ini /home/hc/project/autoAweme.ini
root      59406  0.0  1.3 467760 53104 ?        S    12月04   0:03 /home/hc/project/envs/autoAweme/bin/uwsgi --ini /home/hc/project/autoAweme.ini

3. 查找指定进程个数

ps -ef|grep uwsgi -c
ps -ef|grep -c uwsgi
[root@localhost autoAweme]# ps -ef|grep uwsgi -c
[root@localhost autoAweme]# ps -ef|grep -c uwsgi 

4. 从文件中读取关键词进行搜索

cat 3.log | grep -f 4.log 
[root@localhost test]# cat 3.log 
[root@localhost test]# cat 4.log 
[root@localhost test]# cat 3.log | grep -f 4.log 
[root@localhost test]# cat 4.log | grep -f 3.log 

cat 3.log | grep -f 4.log 从3.log文件中匹配出含有4.log中关键字的行并输出
cat 4.log | grep -f 3.log 从4.log文件中匹配出含有3.log中关键字的行并输出
如:4.log中的关键字有1,12,5,43四个,在3.log中无论是完全匹配还是部分匹配只能匹配到1,并输出
在 3.log中关键字为1,2,3, 所以在4.log中匹配3时,能完全匹配到含有1,2,3的行,并把匹配部分着色表示输出

5. 从文件中读取关键词进行搜索 且显示行号

cat 4.log | grep -nf 3.log
[root@localhost test]# cat 4.log | grep -nf 3.log 
[root@localhost test]# cat 3.log | grep -nf 4.log 

输出4.log文件中含有从3.log 文件中读取出的关键词的内容行,并显示每一行的行号,冒号(:)左边是行号,右边是匹配的内容

6. 从文件中查找关键词

grep "1" 4.log 
[root@localhost test]# cat 4.log 
[root@localhost test]# grep  1  4.log 
[root@localhost test]# grep  '1'  4.log 
[root@localhost test]# grep  "1"  4.log 
[root@localhost test]# grep  \"1\" 4.log 

有无引号,或者单双引号 效果是一样的,但是加上引号可读性好一点。另外如果要查询带引号的内容,需要用\进行转义

7. 从多个文件中查找关键词

grep '1' 3.log 4.log 
[root@localhost test]# grep '1' 3.log 4.log 
3.log:1
4.log:1
4.log:12
4.log:"1"
[root@localhost test]# grep -n 1 3.log 4.log 
3.log:1:1
4.log:1:1
4.log:2:12
4.log:3:"1"

多文件时,输出查询到的信息内容行时,会把文件的命名放在在行的最左边输出并且加上":"作为标示符分隔,如果用了-n展示行号,则第二个:的左边是行号,最右边的是匹配内容

8.找出以1开头的行内容

cat 4.log |grep ^1
[root@localhost test]# cat 4.log 
[root@localhost test]# cat 4.log |grep ^1

9.找出非1开头的行内容

cat 4.log |grep ^[^1]
[root@localhost test]# cat 4.log |grep ^[^1]

10.找出以3结尾的行内容

cat 4.log |grep 3$
[root@localhost test]# cat 4.log |grep 3$

11.在当前目录中,查找后缀有 log 字样的文件中包含 1 字符串的文件,并打印出该字符串的行

 grep 1 *log
[root@localhost test]# ls
1.log  2.log  2.log.back  3.log  4.log
[root@localhost test]# grep 1 *log
3.log:1
4.log:1
4.log:12
4.log:"1"

12 . 以递归的方式查找符合条件的文件

 grep -r 仅此一条 /home/hc
[root@localhost hc]# grep -r 仅此一条 /home/hc
/home/hc/test/2.log.back:仅此一条,我是第五行
[root@localhost hc]# 
查找指定目录/home/hc 及其子目录(如果存在子目录的话)下所有文件中包含字符串"仅此一条"的文件,并打印出该字符串所在行的内容

13.反向查找

grep -v 仅此一条 *
[root@localhost test]# ls
1.log  2.log  2.log.back  3.log  4.log
[root@localhost test]# grep 仅此一条 *
2.log.back:仅此一条,我是第五行
[root@localhost test]# grep -v 仅此一条 *
1.log:hnlinux
1.log:
1.log:hpython.cn
1.log:
1.log:ubuntu
1.log:
1.log:ubuntu linux
1.log:
1.log:redhat
1.log:
1.log:Redhat
1.log:
1.log:linuxmint
2.log:linux
2.log:
2.log:Redhat
2.log.back:第一行
2.log.back:第二行
2.log.back:我是log2第3行
2.log.back:第四行
3.log:1
3.log:2
3.log:3
4.log:1
4.log:12
4.log:"1"
4.log:5
4.log:43
查找当前目录下所有文件,找出其中不包含"仅此一条"的行