天天看点

让你的大文件搜索666的飞起,linux命令grep经典场景(下)进阶

作者:云中漫步

示例

grep命令基本语法格式:

$ grep [option] pattern file           
  • pattern:要搜索的字符模式
  • file:文件名称或路径
  • option:为命令提供额外的功能,例如区分大小写的搜索、正则表达式搜索、递归搜索、计数行等。

1 grep打印匹配的模式及下N行

默认情况下,grep打印匹配模式的行。但有时,您可能需要在匹配模式后打印N行。

-A选项允许在匹配行之后打印N行。

要打印匹配文本mongo后的下3行,执行以下命令。

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep -A 3 mongo friutes.txt
mongos
orages
watermelon
hello world           

2 grep打印匹配的模式和前N行

类似地,用户可以使用-B选项在匹配行之前打印N行。

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep -B 3 mongo friutes.txt

world hello
world is big
mongos           

3 grep并打印匹配后的特定行

下例子grep字符串“Iron”,并在匹配后打印第1、8和10行。

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ cat -n article.txt | grep Iron -A 10 | awk "NR==1{print} NR==8{print} NR==10{print;exit}"
     1  Iron pyrite is the most foolish of all minerals.
     8  He had a vague sense that trees gave birth to dinosaurs.
    10  She looked into the mirror and saw another person.           

4 grep打印模式匹配后面的一个单词

下面的命令,打印每一个匹配的下一个单词。

$ grep -oP '(?<=pattern )[^ ]*' file
或者
$ grep -oP '(?<=pattern )\w+' file           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep -oP '(?<=hello )\w+' friutes.txt
world
again           

类似的,可以打印匹配位置后所有的内容。

$ grep -oP '(?<=pattern ).*' file           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep -oP '(?<=world ).*' friutes.txt
hello
is big
is bigger than you see.           

5 打印模式匹配前的单词

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep -oP '\w+(?= world)' friutes.txt
hello
This           

类似的,可以打印匹配之前的所有内容,命令格式:

grep -oP '.*(?= pattern)' file           

6 使用grep精确匹配(全词匹配)

-w选项可用于匹配整个单词。例如,如果grep the,它将只打印包含整个单词the的行。

命令格式:

$ grep -w the test.txt           

示例,查找big的全词匹配:

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep -w big friutes.txt
world is big
yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep big friutes.txt
world is big
This world is bigger than you see.           

7 打印匹配之后的数个字符

打印匹配后的5个字符。

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep -oP 'big.{0,5}' friutes.txt
big
bigger t           

类似的,可以打印15个:

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep -oP 'big.{0,15}' friutes.txt
big
bigger than you se           

8 打印匹配之前的数个字符

打印world以及之前的5个字符。

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep -oP '.{0,5}world' friutes.txt
world
world
ello world
This world           

9 只打印模式匹配的内容

带-o的grep命令只打印匹配的模式,而不是完整的行。

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep -o world friutes.txt
world
world
world
world           

10 打印文件名称

-H选项打印每个匹配项的文件名。当有多个文件需要搜索模式时,它很有帮助。

命令模式:

$ grep -H pattern file           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep -H mongo friutes.txt*
friutes.txt:mongos
friutes.txt-new:mongos
friutes.txt-new:mongos
friutes.txt-new:mongos
friutes.txt2:mongos           

11 正则模式匹配

用户可以将grep命令与正则表达式(regex)组合在一起,对文件执行高级搜索。例如,下面的命令匹配包含数字0到9的任何行。

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep [0-9] friutes.txt*
friutes.txt:11
friutes.txt:1,2,3           

12 打印行号

-n选项强制grep在开头显示匹配的行及其行号。

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep -n mongo friutes.txt
10:mongos           

13 递归在所有目录和子目录中查询

要在所有目录和子目录中递归地搜索单词,可以使用-R、-r或——recursive选项。例如,下面的示例在/tmp/grepdir目录及其子目录中递归地执行grep。

命令格式:

$ grep -r mongo /tmp/grepdir/           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep -r mongo /tmp/grepdir/
/tmp/grepdir/friutes.txt-new:mongos
/tmp/grepdir/friutes.txt-new:mongos
/tmp/grepdir/friutes.txt-new:mongos
/tmp/grepdir/friutes.txt:mongos
/tmp/grepdir/friutes.txt2:mongos
/tmp/grepdir/dir1/friutes.txt:mongos
/tmp/grepdir/dir1/dir11/friutes.txt:mongos           

14 grep递归只到目录的一定深度

用户可以使用grep命令和find命令来执行递归搜索,直到目录中的某个深度。

命令格式:

$ find /tmp/grepdir -maxdepth 2 -type f -exec grep -H mongo {} \;           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ find /tmp/grepdir -maxdepth 2 -type f -exec grep -H mongo {} \;
/tmp/grepdir/friutes.txt-new:mongos
/tmp/grepdir/friutes.txt-new:mongos
/tmp/grepdir/friutes.txt-new:mongos
/tmp/grepdir/friutes.txt:mongos
/tmp/grepdir/friutes.txt2:mongos
/tmp/grepdir/dir1/friutes.txt:mongos           

15 grep模式并从文件中删除一行

用户可以使用grep命令从文件中删除匹配的行。下面的命令在grep和mv命令的帮助下从friutes.txt中删除匹配的行。

实现原理:用grep -v去掉所有的匹配行,输出内容到新文件。

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep -v mongo friutes.txt > tempfile && mv tempfile friutes.txt.new
yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ cat friutes.txt.new
apples

bananas

pears
11
1,2,3
world hello
world is big
orages
watermelon
hello world
hello again
This world is bigger than you see.           

16 带空格的模式

可以通过使用正则,匹配带空格的模式。

命令格式:

$ grep -E "(^| )hours( |$)" test2.txt           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep -E "(^| )world( |$)" friutes.txt
world hello
world is big
hello world
This world is bigger than you see.           

17 grep以模式开头行

插入号()表示行开始。要搜索以S开头的行,可以使用S正则表达式。

命令格式:

$ grep '^S' test.txt           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep '^T' friutes.txt
This world is bigger than you see.           

18 grep以模式结尾的行

类似地,您可以使用美元符号$来搜索以特定模式结束的行。

这个命令查找以d结尾的行。

命令格式:

$ grep 'd#39; test4.txt            

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep 'big#39; friutes.txt
world is big           

19 grep 多个模式或

你可以使用-E选项在OR条件下grep多个模式。用户必须用|符号分隔图案。

它输出包含匹配模式之一的行。

命令格式:

$ grep -E 'pattern1|pattern2' file           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep -E 'world|big' friutes.txt
world hello
world is big
hello world
This world is bigger than you see.           

也可以使用多个-e参数,效果与上面一致:

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep -e world -e big  friutes.txt
world hello
world is big
hello world
This world is bigger than you see.           

20 多个条件与查询

要使用带有AND条件的grep命令,请使用以下语法。

命令格式:

$ grep -P '^(?=.*pattern1)(?=.*pattern2)' file           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep -P '^(?=.*hello)(?=.*world)' friutes.txt
world hello
hello world           

21 grepIP地址

下面的grep命令从指定文件中获取所有有效的IPv4地址。

$ grep -E "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" file           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ ip address > ifconfig
yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep -E "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" ifconfig
    inet 127.0.0.1/8 scope host lo
    link/ipip 0.0.0.0 brd 0.0.0.0
    link/sit 0.0.0.0 brd 0.0.0.0
    inet 172.24.170.80/20 brd 172.24.175.255 scope global eth0           

22 只包含字母字符的查询

您可以使用以下命令只匹配字母字符的字符串。命令格式:

$ grep -E "^[a-zA-Z-]*[a-zA-Z]#34; file           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep -E "^[a-zA-Z-]*[a-zA-Z]#34; friutes.txt
apples
bananas
pears
mongos
orages
watermelon
yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ cat friutes.txt
apples

bananas

pears
11
1,2,3
world hello
world is big
mongos
orages
watermelon
hello world
hello again
This world is bigger than you see.           

23 只包含字母数字字符的查询

命令格式如下:

$ grep -Ei '[a-z][0-9]|[0-9][a-z]' file
           

24 特殊字符(括号,点,冒号,引号,通配符等)的Grep字符串

我们可以为grep提供使用单引号的特殊字符列表。这里有一个带有一些特殊字符的示例文件。

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep -E '\\|\/|\[|\]|\"|\.' test.char
Opening bracket [
Closing bracket ]
Dot .
\ back slash
/ forward slash
" double quotes
yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ cat test.char
Opening bracket [
Closing bracket ]
Dot .
* asterisk
\ back slash
/ forward slash
" double quotes
' single quotes           

我们还可以在grep中使用以下选项来查找特殊字符:

  • -F , --fixed-strings: 将pattern解释为固定的字符串,而不是正则表达式。
  • -G, --basic-regexp: 将模式解释为基本的正则表达式

    下面是一个grep反斜杠的例子。

grep -F '\' test1.txt 
           

但是你应该知道-F或-G不能和-E结合。在这种情况下,将会抛出错误:grep: conflicting matchers specified.

25 查询中间为tab的两个单词

您可以使用下面的grep命令来搜索两个中间带有TAB的单词。

命令格式:

$ grep 'word1'#39;\t''word2' file
           

示例:

yunzhong@DESKTOP-9VB7LN7:/tmp/grepdir$ grep 'hello'#39;\t''world' friutes.txt.new
hello   world with tab           

继续阅读