当前位置: 首页 > news >正文

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

Linux 文本搜索利器: grep命令

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

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

grep 命令语法

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

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

环境准备:

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

grep 命令选项详解

模式选择和解释选项

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

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

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

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

[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

[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 选项:忽略大小写

忽略大小写匹配。

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

匹配整个单词。

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

匹配整行。

[furongwang@shell ~]$ cat words | grep -x 'cat'
# 或者
[furongwang@shell ~]$ cat words | grep '^cat$'
cat

输出控制选项

-v 选项:反向匹配

显示与PATTERNS不匹配的项目。

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

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

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

显示匹配到项目的数量。

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

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

[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 选项:显示行号

显示匹配项目的行号。

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

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

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

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

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

# 找到的情况
[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 选项:抑制错误信息

不显示任何错误输出。

[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,递归匹配目录,跟随软链接
[furongwang@shell ~]$ grep -r '^SELINUX=' -s /etc
/etc/selinux/config:SELINUX=disabled
-h 和 -H 选项:文件名显示控制
  • -h,不显示匹配项目所在文件的文件名
  • -H,显示匹配项目所在文件的文件名,默认情况使用该选项
[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,对目录匹配时,只显示那些不包含匹配模式的文件的名称
[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 选项:显示前几行

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

[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 1000link/ether 00:0c:29:2b:c8:7a brd ff:ff:ff:ff:ff:ffinet 10.1.8.88/24 brd 10.1.8.255 scope global noprefixroute ens32
-A 选项:显示后几行

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

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

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

[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 1000link/ether 00:0c:29:2b:c8:7a brd ff:ff:ff:ff:ff:ffinet 10.1.8.88/24 brd 10.1.8.255 scope global noprefixroute ens32valid_lft forever preferred_lft foreverinet6 fe80::5882:62aa:161b:c9c1/64 scope link tentative noprefixroute dadfailed

附:grep 命令完整帮助信息

[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 newlineMiscellaneous:-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 exitOutput 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-recursivelikewise, 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 nameContext 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/>
http://www.dtcms.com/a/341781.html

相关文章:

  • JavaBean中首字母小写第二个字母大写属性转换异常详解
  • GIT总结一键式命令清单(顺序执行)
  • redis---常用数据类型及内部编码
  • 官网SSO登录系统的企业架构设计全过程
  • 七十四、【Linux数据库】MySQL数据库存储引擎
  • 11让LLM更懂FunctionCalling返回值
  • S32K3 的图形化配置和EB配置mcal差异
  • week2-[二维数组]排队
  • MySQL/Kafka数据集成同步,增量同步及全量同步
  • Windows 如何清理右键菜单?电脑桌面右键菜单里出现一个清理内存 怎么去掉?
  • 数据结构中邻接矩阵中的无向图和有向图
  • 流固耦合|01流固耦合分类
  • 面试 TOP101 二分查找/排序专题题解汇总Java版(BM17 —— BM22)
  • Alpha测试:软件上线前的关键环节
  • 意象框架:连接感知与认知的统一信息结构分析——基于上古汉语同源词意义系统的词源学与认知语言学探索
  • 深入理解与应用向量嵌入(Vector Embeddings):原理、实现与多场景实践
  • 轻量级流程编排框架,Solon Flow v3.5.0 发布
  • WEB安全篇:浏览器攻击原理及防护
  • 软件设计师——数据结构与算法基础学习笔记
  • mac安装Trae并解决App Unavailable问题
  • 【Java进阶】Java JIT 编译器深度解析与优化实践
  • 49.Seata-XA模式
  • Day57 Java面向对象12 多态
  • 齐次线性方程组最小二乘解
  • 压缩包密码找回工具递归解压增强版使用说明
  • 机器学习数据预处理学习报告
  • Linux用30秒部署Nginx+Tomcat+Mysql+Jdk1.8环境
  • Paging in Operating System
  • windows server 彻底卸载oracle 11g
  • Linux命令大全-ps命令