【网络运维】Linux 文本搜索利器: grep命令

Linux 文本搜索利器: grep命令

grep 是 Linux 系统中最重要的命令之一,其功能是从文本文件或管道数据流中筛选匹配的行及数据。

本文将深入探讨 grep 命令的使用方法和实用技巧。

grep 命令语法

grep 命令有两种基本使用形式:

  1. 过滤管道数据:command | grep [OPTION]... PATTERNS
  2. 过滤文件内容:grep [OPTION]... PATTERNS [FILE]...

环境准备:

bash 复制代码
[furongwang@shell ~]$ vim words
cat
category
acat
concatenate
dog
cbt
c1t
cCt
c.t
dogdog
dogdogdog
dogdogdogdog

grep 命令选项详解

模式选择和解释选项

-E 选项:扩展正则表达式

支持扩展正则表达式,相当于 egrep 命令。

bash 复制代码
[furongwang@shell ~]$ cat words | grep -E '(dog){3}'
# 或者
[furongwang@shell ~]$ cat words | egrep '(dog){3}'
dogdogdog
dogdogdogdog
-e 选项:多模式匹配

使用多个 -e 选项匹配多个PATTERNS

bash 复制代码
[furongwang@shell ~]$ cat words | grep -e 'cat' -e 'dog'
# 或者
[furongwang@shell ~]$ cat words | egrep 'cat|dog'
cat
category
acat
concatenate
dog
dogdog
dogdogdog
dogdogdogdog
hello cat
-f 选项:从文件读取模式

从文件读取多个 PATTERNS

bash 复制代码
[furongwang@shell ~]$ echo -e 'cat\ndog' > pattens_file
[furongwang@shell ~]$ cat pattens_file
cat
dog
[furongwang@shell ~]$ cat words | grep -f pattens_file 
cat
category
acat
concatenate
dog
dogdog
dogdogdog
dogdogdogdog
hello cat
-i 选项:忽略大小写

忽略大小写匹配。

bash 复制代码
[furongwang@shell ~]$ cat words | grep -i 'cBt'
cbt
-w 选项:整词匹配

匹配整个单词。

bash 复制代码
[furongwang@shell ~]$ cat words | grep -w 'cat'
# 或者
[furongwang@shell ~]$ cat words | grep '\bcat\b'
cat
hello cat
-x 选项:整行匹配

匹配整行。

bash 复制代码
[furongwang@shell ~]$ cat words | grep -x 'cat'
# 或者
[furongwang@shell ~]$ cat words | grep '^cat$'
cat

输出控制选项

-v 选项:反向匹配

显示与PATTERNS不匹配的项目。

bash 复制代码
[furongwang@shell ~]$ cat words | egrep -v '^d|^c'
acat
hello cat

# 不看注释行和空白行
[furongwang@shell ~]$ egrep -v '^ *#|^$' /etc/profile
-m 选项:限制匹配数量

控制最大匹配数目,匹配特定次数后停止匹配。

bash 复制代码
[furongwang@shell ~]$ cat words | grep 'dog'
dog
dogdog
dogdogdog
dogdogdogdog

[furongwang@shell ~]$ cat words | grep -m2 'dog'
dog
dogdog
-c 选项:计数匹配行

显示匹配到项目的数量。

bash 复制代码
[furongwang@shell ~]$ cat words | grep -c 'dog'
4
-b 选项:显示字节偏移量

显示匹配项目的字节偏移量。

bash 复制代码
[furongwang@shell ~]$ head -5 words 
cat
category
acat
concatenate
dog

[furongwang@shell ~]$ cat words | grep -b 'cat'
0:cat
4:category
13:acat
18:concatenate
109:hello cat
-n 选项:显示行号

显示匹配项目的行号。

bash 复制代码
[furongwang@shell ~]$ cat words | grep -n 'cat'
1:cat
2:category
3:acat
4:concatenate
19:hello cat
-o 选项:仅显示匹配内容

只显示匹配到的内容,行中其他内容不显示。

bash 复制代码
[furongwang@shell ~]$ cat words | egrep '(dog){3}'
dogdogdog
dogdogdogdog

[furongwang@shell ~]$ cat words | egrep -o '(dog){3}'
dogdogdog
dogdogdog
-q 选项:静默模式

不显示任何正常输出。一般用于脚本判定文件中是否包含特定内容。

通过特殊变量 $? 查看是否匹配到内容。

bash 复制代码
# 找到的情况
[furongwang@shell ~]$ cat words | egrep -q '(dog){3}'
[furongwang@shell ~]$ echo $?
0

# 找不到的情况
[furongwang@shell ~]$ cat words | egrep -q '(dog){3}xuebao'
[furongwang@shell ~]$ echo $?
1
-s 选项:抑制错误信息

不显示任何错误输出。

bash 复制代码
[furongwang@shell ~]$ grep '^SELINUX=' /etc/shadow /etc/selinux/config 
grep: /etc/shadow: Permission denied
/etc/selinux/config:SELINUX=disabled

[furongwang@shell ~]$ grep -s '^SELINUX=' /etc/shadow /etc/selinux/config 
/etc/selinux/config:SELINUX=disabled

查找文件选项

-r 和 -R 选项:递归搜索
  • -r,递归匹配目录
  • -R,递归匹配目录,跟随软链接
bash 复制代码
[furongwang@shell ~]$ grep -r '^SELINUX=' -s /etc
/etc/selinux/config:SELINUX=disabled
-h 和 -H 选项:文件名显示控制
  • -h,不显示匹配项目所在文件的文件名
  • -H,显示匹配项目所在文件的文件名,默认情况使用该选项
bash 复制代码
[furongwang@shell ~]$ grep -r '^SELINUX=' -s -h /etc
SELINUX=disabled

[furongwang@shell ~]$ grep -r '^SELINUX=' -s -H /etc
/etc/selinux/config:SELINUX=disabled
-l 和 -L 选项:文件列表输出
  • -l,对目录匹配时,只显示那些包含匹配模式的文件的名称
  • -L,对目录匹配时,只显示那些不包含匹配模式的文件的名称
bash 复制代码
[furongwang@shell ~]$ grep -r '^SELINUX=' -s -l /etc
/etc/selinux/config

[furongwang@shell ~]$ grep -r '^SELINUX=' -s -L /etc | tail -5
/etc/gdm/PostLogin/Default.sample
/etc/gdm/PostSession/Default
/etc/gdm/PreSession/Default
/etc/gdm/custom.conf
/etc/nfs.conf

输出内容控制选项

-B 选项:显示前几行

显示匹配项目本身,以及前多少行。

bash 复制代码
[furongwang@shell ~]$ ip addr | grep '10.1.8.88' -B2
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:2b:c8:7a brd ff:ff:ff:ff:ff:ff
    inet 10.1.8.88/24 brd 10.1.8.255 scope global noprefixroute ens32
-A 选项:显示后几行

显示匹配项目本身,以及后多少行。

bash 复制代码
[furongwang@shell ~]$ ip addr |grep 'ens32:' -A2
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:2b:c8:7a brd ff:ff:ff:ff:ff:ff
    inet 10.1.8.88/24 brd 10.1.8.255 scope global noprefixroute ens32
-C 选项:显示前后几行

显示匹配项目本身,以及前后多少行。

bash 复制代码
[furongwang@shell ~]$ ip addr |grep '10.1.8.88' -C2
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:2b:c8:7a brd ff:ff:ff:ff:ff:ff
    inet 10.1.8.88/24 brd 10.1.8.255 scope global noprefixroute ens32
       valid_lft forever preferred_lft forever
    inet6 fe80::5882:62aa:161b:c9c1/64 scope link tentative noprefixroute dadfailed

附:grep 命令完整帮助信息

bash 复制代码
[furongwang@shell ~]$ grep --help
Usage: grep [OPTION]... PATTERNS [FILE]...
Search for PATTERNS in each FILE.
Example: grep -i 'hello world' menu.h main.c
PATTERNS can contain multiple patterns separated by newlines.

Pattern selection and interpretation:
  -E, --extended-regexp     PATTERNS are extended regular expressions
  -F, --fixed-strings       PATTERNS are strings
  -G, --basic-regexp        PATTERNS are basic regular expressions
  -P, --perl-regexp         PATTERNS are Perl regular expressions
  -e, --regexp=PATTERNS     use PATTERNS for matching
  -f, --file=FILE           take PATTERNS from FILE
  -i, --ignore-case         ignore case distinctions in patterns and data
      --no-ignore-case      do not ignore case distinctions (default)
  -w, --word-regexp         match only whole words
  -x, --line-regexp         match only whole lines
  -z, --null-data           a data line ends in 0 byte, not newline

Miscellaneous:
  -s, --no-messages         suppress error messages
  -v, --invert-match        select non-matching lines
  -V, --version             display version information and exit
      --help                display this help text and exit

Output control:
  -m, --max-count=NUM       stop after NUM selected lines
  -b, --byte-offset         print the byte offset with output lines
  -n, --line-number         print line number with output lines
      --line-buffered       flush output on every line
  -H, --with-filename       print file name with output lines
  -h, --no-filename         suppress the file name prefix on output
      --label=LABEL         use LABEL as the standard input file name prefix
  -o, --only-matching       show only nonempty parts of lines that match
  -q, --quiet, --silent     suppress all normal output
      --binary-files=TYPE   assume that binary files are TYPE;
                            TYPE is 'binary', 'text', or 'without-match'
  -a, --text                equivalent to --binary-files=text
  -I                        equivalent to --binary-files=without-match
  -d, --directories=ACTION  how to handle directories;
                            ACTION is 'read', 'recurse', or 'skip'
  -D, --devices=ACTION      how to handle devices, FIFOs and sockets;
                            ACTION is 'read' or 'skip'
  -r, --recursive           like --directories=recurse
  -R, --dereference-recursive
                            likewise, but follow all symlinks
      --include=GLOB        search only files that match GLOB (a file pattern)
      --exclude=GLOB        skip files that match GLOB
      --exclude-from=FILE   skip files that match any file pattern from FILE
      --exclude-dir=GLOB    skip directories that match GLOB
  -L, --files-without-match print only names of FILEs with no selected lines
  -l, --files-with-matches  print only names of FILEs with selected lines
  -c, --count               print only a count of selected lines per FILE
  -T, --initial-tab         make tabs line up (if needed)
  -Z, --null                print 0 byte after FILE name

Context control:
  -B, --before-context=NUM  print NUM lines of leading context
  -A, --after-context=NUM   print NUM lines of trailing context
  -C, --context=NUM         print NUM lines of output context
  -NUM                      same as --context=NUM
      --group-separator=SEP use SEP as a group separator
      --no-group-separator  use empty string as a group separator
      --color[=WHEN],
      --colour[=WHEN]       use markers to highlight the matching strings;
                            WHEN is 'always', 'never', or 'auto'
  -U, --binary              do not strip CR characters at EOL (MSDOS/Windows)

When FILE is '-', read standard input.  With no FILE, read '.' if
recursive, '-' otherwise.  With fewer than two FILEs, assume -h.
Exit status is 0 if any line is selected, 1 otherwise;
if any error occurs and -q is not given, the exit status is 2.

Report bugs to: bug-grep@gnu.org
GNU grep home page: <http://www.gnu.org/software/grep/>
General help using GNU software: <https://www.gnu.org/gethelp/>
相关推荐
byte轻骑兵18 分钟前
【Linux文件系统】Linux文件系统与设备驱动
linux·运维·服务器
Lethehong3 小时前
在 CentOS 7 上搭建 OpenTenBase 集群:从源码到生产环境的全流程指南
linux·运维·centos·tdsql·opentenbase·腾讯云数据库
Hard but lovely3 小时前
vim的使用
linux·编辑器·vim
yuxb733 小时前
集群与负载均衡:HAProxy 与 Nginx 实践
运维·nginx·负载均衡
知白守黑2673 小时前
KVM虚拟化
linux·运维·架构·centos
.Shu.10 小时前
计算机网络 TLS握手中三个随机数详解
网络·计算机网络·安全
程序员 _孜然14 小时前
Ubuntu/Debian修改网卡名字enP3p49s0为eth0
linux·运维·驱动开发·嵌入式硬件·ubuntu·debian
IDIOT___IDIOT14 小时前
Linux mount 命令
linux·运维·服务器
暗流者14 小时前
AAA 服务器与 RADIUS 协议笔记
运维·服务器·笔记