grep的使用
最近经常需要到Linux上查日志,于是重新学习了一下grep命令,下面做个笔记。
grep
是Linux上非常常见的文本搜索工具,如果一个人说他不会用grep,那么他肯定不会Linux。
1. grep-输出匹配的行
案例
cd /usr/share/centos-release
cat EULA
CentOS 7 Linux EULA
CentOS 7 Linux comes with no guarantees or warranties of any sorts,
either written or implied.
The Distribution is released as GPLv2. Individual packages in the
distribution come with their own licences. A copy of the GPLv2 license
is included with the distribution media.
常见用法如下:
1. 不区分大小写,逐字符匹配。
[root@localhost centos-release]# grep -i linux EULA
CentOS 7 Linux EULA
CentOS 7 Linux comes with no guarantees or warranties of any sorts,
2. 匹配内容添加行数。
[root@localhost centos-release]# grep -n -i linux EULA
1:CentOS 7 Linux EULA
3:CentOS 7 Linux comes with no guarantees or warranties of any sorts,
3. 打印匹配内容的前几行,后几行或者前后几行。
- A After,后几行
- B before 前几行
- C context 上下几行
[root@localhost centos-release]# grep -A 2 'Dis' EULA
The Distribution is released as GPLv2. Individual packages in the
distribution come with their own licences. A copy of the GPLv2 license
is included with the distribution media.
[root@localhost centos-release]# grep -B 2 'Dis' EULA
either written or implied.
The Distribution is released as GPLv2. Individual packages in the
[root@localhost centos-release]# grep -C 2 'Dis' EULA
either written or implied.
The Distribution is released as GPLv2. Individual packages in the
distribution come with their own licences. A copy of the GPLv2 license
is included with the distribution media.
4. 使用正则匹配。这个时候可以使用egrep命令
4.1 仅仅匹配以The开头的行
[root@localhost centos-release]# egrep '^The' EULA
The Distribution is released as GPLv2. Individual packages in the
4.2 精确匹配某个单词。使用-w或者egrep
[root@localhost centos-release]# egrep '\bcome\b' EULA
distribution come with their own licences. A copy of the GPLv2 license
[root@localhost centos-release]# egrep '\bcomes\b' EULA
CentOS 7 Linux comes with no guarantees or warranties of any sorts,
[root@localhost centos-release]# grep -w come EULA
distribution come with their own licences. A copy of the GPLv2 license
4.3 模糊匹配一些字符。.代表任意字符,包括空格。
[root@localhost centos-release]# egrep 'Dis...' EULA
The Distribution is released as GPLv2. Individual packages in the
4.4 []查询,查询In或on
[root@localhost centos-release]# egrep '[Io]n' EULA
The Distribution is released as GPLv2. Individual packages in the
distribution come with their own licences. A copy of the GPLv2 license
is included with the distribution media.
5. 重复次数查询,或者量词查询。
正则表达式可能重复几次撇皮
? 前面的字符是可选的和只匹配一次
- 前面的字符可以匹配0此或者更多次
- 前面的字符至少匹配一次,可以匹配更多次
{n} 前面的字符精确匹配 n 此
{n,}前面的字符匹配n此以上
{,m}前面的字符至多匹配m次
():向后引用,引用:\1, \2, \3
[root@localhost centos-release]# egrep 'e{2}' EULA
CentOS 7 Linux comes with no guarantees or warranties of any sorts,
6. 或者匹配,匹配条件中任选其一。
[root@localhost centos-release]# egrep '(or|of)' EULA
CentOS 7 Linux comes with no guarantees or warranties of any sorts,
either written or implied.
distribution come with their own licences. A copy of the GPLv2 license
7. 递归搜索目录下是否有文件符合自己想要的内容。
cd /usr/local/openresty/nginx/conf/
[root@localhost conf]# grep -n -r '\$server_protocol' ./
./fastcgi.conf:12:fastcgi_param SERVER_PROTOCOL $server_protocol;
./fastcgi.conf.default:12:fastcgi_param SERVER_PROTOCOL $server_protocol;
./fastcgi_params:11:fastcgi_param SERVER_PROTOCOL $server_protocol;
./fastcgi_params.default:11:fastcgi_param SERVER_PROTOCOL $server_protocol;
./scgi_params:10:scgi_param SERVER_PROTOCOL $server_protocol;
./scgi_params.default:10:scgi_param SERVER_PROTOCOL $server_protocol;
./uwsgi_params:10:uwsgi_param SERVER_PROTOCOL $server_protocol;
./uwsgi_params.default:10:uwsgi_param SERVER_PROTOCOL $server_protocol;
以上就是在使用Linux过程中,常用到的grep命令Demo。