grep 搜索文件中的字符串

作用

grep 命令可以使用正则表达式搜索文本,并把匹配的行打印出来。它一般用于对其他命令的输出结果进行过滤,查找特定字符串,匹配一定模式的字符串在文件中的位置。

语法

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

参数

  • -a或--text   不要忽略二进制的数据。

  • -A<显示列数>或--after-context=<显示列数>   除了显示符合范本样式的那一列之外,并显示该列之后的内容。

  • -b或--byte-offset   在显示符合范本样式的那一列之前,标示出该列第一个字符的位编号。

  • -B<显示列数>或--before-context=<显示列数>   除了显示符合范本样式的那一列之外,并显示该列之前的内容。

  • -c或--count   计算符合范本样式的列数。

  • -C<显示列数>或--context=<显示列数>或-<显示列数>   除了显示符合范本样式的那一列之外,并显示该列之前后的内容。

  • -d<进行动作>或--directories=<进行动作>   当指定要查找的是目录而非文件时,必须使用这项参数,否则grep指令将回报信息并停止动作。

  • -e<范本样式>或--regexp=<范本样式>   指定字符串做为查找文件内容的范本样式。

  • -E或--extended-regexp   将范本样式为延伸的普通表示法来使用。

  • -f<范本文件>或--file=<范本文件>   指定范本文件,其内容含有一个或多个范本样式,让grep查找符合范本条件的文件内容,格式为每列一个范本样式。

  • -F或--fixed-regexp   将范本样式视为固定字符串的列表。

  • -G或--basic-regexp   将范本样式视为普通的表示法来使用。

  • -h或--no-filename   在显示符合范本样式的那一列之前,不标示该列所属的文件名称。

  • -H或--with-filename   在显示符合范本样式的那一列之前,表示该列所属的文件名称。

  • -i或--ignore-case   忽略字符大小写的差别。

  • -l或--file-with-matches   列出文件内容符合指定的范本样式的文件名称。

  • -L或--files-without-match   列出文件内容不符合指定的范本样式的文件名称。

  • -n或--line-number   在显示符合范本样式的那一列之前,标示出该列的列数编号。

  • -q或--quiet或--silent   不显示任何信息。

  • -r或--recursive   此参数的效果和指定“-d recurse”参数相同。

  • -s或--no-messages   不显示错误信息。

  • -v或--revert-match   反转查找。

  • -V或--version   显示版本信息。

  • -w或--word-regexp   只显示全字符合的列。

  • -x或--line-regexp   只显示全列符合的列。

  • -y   此参数的效果和指定“-i”参数相同。

  • --help   在线帮助。

示例

(1)使用 grep 命令过滤“ls”的显示内容,如下:

[hxstrive@localhost ~]$ ls -l | grep D
drwxr-xr-x. 2 hxstrive hxstrive        6 Apr  2  2022 Desktop
drwxr-xr-x. 2 hxstrive hxstrive        6 Apr  2  2022 Documents
drwxr-xr-x. 2 hxstrive hxstrive        6 Oct  4 23:02 Downloads

上述命令使用 grep 过滤 ls -l 命令输出结果中包含字符串“D”的输出结果。

(2)使用 grep 命令显示指定进程信息,如下:

[hxstrive@localhost ~]$ ps -ef | grep sshd
root         853       1  0 Nov11 ?        00:00:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
hxstrive   49955   48906  0 15:19 pts/0    00:00:00 grep --color=auto sshd

上面使用“ps -ef”命令显示所有进程的简单信息,然后利用 grep 对输出结果进行过滤,只显示包含 sshd 字符串的结果。

(3)查找某个文件中的特殊字符串,如下:

# ifconfig 文件存放了当前机器执行 ifconfig 命令的结果
[hxstrive@localhost ~]$ cat ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.116.132  netmask 255.255.255.0  broadcast 192.168.116.255
        inet6 fe80::20c:29ff:fe83:e380  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:83:e3:80  txqueuelen 1000  (Ethernet)
        RX packets 2037288  bytes 3014492964 (2.8 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 184680  bytes 11707540 (11.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 242  bytes 26608 (25.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 242  bytes 26608 (25.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

# 过滤 ifconfig 文件中包含“packets”字符串的行
[hxstrive@localhost ~]$ grep -n "packets" ifconfig 
5:        RX packets 2037288  bytes 3014492964 (2.8 GiB)
7:        TX packets 184680  bytes 11707540 (11.1 MiB)
14:        RX packets 242  bytes 26608 (25.9 KiB)
16:        TX packets 242  bytes 26608 (25.9 KiB)

(4)grep 命令通过正则表达式过滤 ls -l 命令的结果。

a、查找以 d 开头的输出行

[hxstrive@localhost ~]$ ls -l | grep ^d
drwxr-xr-x. 2 hxstrive hxstrive        6 Apr  2  2022 Desktop
drwxr-xr-x. 2 hxstrive hxstrive        6 Apr  2  2022 Documents
drwxr-xr-x. 2 hxstrive hxstrive        6 Oct  4 23:02 Downloads
drwxr-xr-x. 2 hxstrive hxstrive        6 Apr  2  2022 Music
drwxr-xr-x. 2 hxstrive hxstrive        6 Apr  2  2022 Pictures
drwxr-xr-x. 2 hxstrive hxstrive        6 Apr  2  2022 Public
drwxrwxr-x. 6 hxstrive hxstrive     4096 Oct  4  2021 redis-5.0.14
drwxr-xr-x. 2 hxstrive hxstrive        6 Apr  2  2022 Templates
drwxr-xr-x. 2 hxstrive hxstrive        6 Apr  2  2022 Videos

b、查找以 s 结尾的输出行

[hxstrive@localhost ~]$ ls -l | grep s$
drwxr-xr-x. 2 hxstrive hxstrive        6 Apr  2  2022 Documents
drwxr-xr-x. 2 hxstrive hxstrive        6 Oct  4 23:02 Downloads
drwxr-xr-x. 2 hxstrive hxstrive        6 Apr  2  2022 Pictures
drwxr-xr-x. 2 hxstrive hxstrive        6 Apr  2  2022 Templates
drwxr-xr-x. 2 hxstrive hxstrive        6 Apr  2  2022 Videos

c、查找以非 d 开头的输出行

[hxstrive@localhost ~]$ ls -l | grep ^[^d]
total 51284
-rw-rw-r--. 1 hxstrive hxstrive        6 Oct  4 23:06 demo
-rw-rw-r--. 1 hxstrive hxstrive       40 Aug 29 00:02 demo1
-rw-rw-r--. 1 hxstrive hxstrive       41 Aug 29 00:02 demo2
-rw-rw-r--. 1 hxstrive hxstrive      903 Nov 12 15:24 ifconfig
-rw-rw-r--. 1 hxstrive hxstrive      761 Nov 12 15:23 localhost
-rw-r--r--. 1 hxstrive hxstrive 52485058 Oct  4 23:01 MiFlash2018-5-28-0.zip
-rw-rw-r--. 1 root     root            6 Oct  7 17:51 readme.txt

更多关于命令详细参考手册,请使用 man 命令或者 --help 参数获取帮助信息

关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号