Linux之grep

1. 递归搜索文件

$ grep "text" . -R -n

命令中的" . " 指定了当前目录

2. 在grep搜索中包括或排除文件

such as 只在目录中递归搜索所有的.java和.class文件:

$ grep "VertexInputFormat" . -r --include *.{java,class}

3. 在搜索中排除所有的README文件

$ grep "license" . -r --exclude "README"

4. 在文件中搜索某个单词

$ grep match_word match_file

5. 在多个文件中搜索某个单词

$ grep match_word match_file1 match_file2 match_file3 ...

6. 在查询的文件目录中进行相关匹配

$ ls -l | grep "Giraph*"

grep之正则表达式相关实例:

实例1:匹配logwebsite中的IP地址

landen@landen-Lenovo:~/文档$ grep -o -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" logwebsite
192.168.194.28
3.9.1.430
192.168.194.92
125.226.150.44
125.226.150.44
125.226.150.44
landen@landen-Lenovo:~/文档$ grep -o -E "[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}" logwebsite
192.168.194.28
3.9.1.430
192.168.194.92
125.226.150.44
125.226.150.44
125.226.150.44
实例2:匹配logwebsite中的相关内容
landen@landen-Lenovo:~/文档$ grep -E -o -n -H "Mozilla/4.0 \([^)]*\)" logwebsite
logwebsite:5:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Tri dent/4.0; SMT Player 3.9.1.430; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
logwebsite:7:Mozilla/4.0 (compatible; MSIE 8.00; Windows 7)
logwebsite:25:Mozilla/4.0 (compatible; MSIE 8.00; Windows 7)

原文地址:https://www.cnblogs.com/likai198981/p/3131780.html