Linux “grep“ 命令

1. 目标

本文主要介绍 Linux "grep" 命令:用于查找文件里符合条件的字符串或正则表达式。

2. grep 命令

"grep" 命令的基本语法是:

复制代码
grep [options] pattern [files]
  • pattern - 表示要查找的字符串或正则表达式。

  • files - 表示要查找的文件名,可以同时查找多个文件,如果省略 files 参数,则默认从标准输入中读取数据。

常用参数说明:

  • -c : 打印与模式匹配的行数;

  • -h : 显示匹配的行,但不显示文件名;

  • -i : 忽略字符大小写的差别;

  • -l : 只显示文件名列表;

  • -n : 显示匹配的行及其行号;

  • -v : 显示不包含匹配文本的所有行;

  • -e exp : 指定字符串作为查找文件内容的样式;

  • -f file : 指定规则文件,其内容含有一个或多个规则样式,让grep查找符合规则条件的文件内容,格式为每行一个规则样式;

  • -E : 将样式为延伸的正则表达式来使用;

  • -w : 显示全匹配整个单词;

  • -o : 只打印匹配行的匹配部分、每个匹配部分都在单独的输出行上;

  • -A n : 打印搜索行和结果后的 n 行;

  • -B n : 打印搜索行和结果前的 n 行;

  • -C n : 打印搜索行及其前后的 n 行;

2.1 示例

备注: 示例文本文件:text.txt

复制代码
root@dev:~/linux# cat > text.txt 
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
learn operating system.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

(1)不区分大小写搜索

通过 -i 选项,可以在给定文件中搜索不区分大小写的字符串。例如:匹配 "UNIX"、"Unix"、"unix "等词。

复制代码
root@dev:~/linux# grep -i "UNix"  text.txt 
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

(2)显示匹配次数

找出与给定字符串/模式匹配的行数。

复制代码
root@dev:~/linux# grep -c "unix" text.txt 
-------------------------------------------------------------------
2

(3)显示与模式匹配的文件名

只需显示包含给定字符串/模式的文件即可。

复制代码
root@dev:~/linux# grep -l "unix" *

或者

root@dev:~/linux# grep -l "unix" text.txt text1.txt text2.txt

输出:
text.txt

(4)检查文件中的整个字符串

默认情况下,grep 会匹配给定的字符串/模式,即使该字符串/模式在文件中是子串。grep 的 -w选项使其只匹配整个单词。

复制代码
root@dev:~/linux# grep -w "unix" text.txt 
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

(5)只显示匹配的模式 默认情况下,grep 会显示匹配字符串的整行。我们可以使用 -o 选项让 grep 只显示匹配的字符串。

复制代码
root@dev:~/linux# grep -o "unix" text.txt 
-------------------------------------------------------------------
unix
unix
unix
unix
unix

(6)使用 grep -n 输出时显示行号

显示匹配行的文件行号。

复制代码
root@dev:~/linux# grep -n "unix" text.txt 
-------------------------------------------------------------------
1:unix is great os. unix was developed in Bell labs.
4:uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

(7)反向模式匹配

使用 -v 选项可以显示与指定搜索字符串模式不匹配的行。

复制代码
root@dev:~/linux# grep -v -n "unix" text.txt 
-------------------------------------------------------------------
2:learn operating system.
3:Unix linux which one you choose.

(8)匹配以字符串开头的行

^ 正则表达式指定一行的开头。在 grep 中使用它可以匹配以给定字符串或正则匹配开头的行。

复制代码
root@dev:~/linux# grep "^unix" text.txt 
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.

(9)匹配以字符串结尾的行

$ 正则表达式指定一行的结尾。在 grep 中可以使用它来匹配以给定字符串或正则表达式结尾的行。

复制代码
root@dev:~/linux# grep "labs.$" text.txt 
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.

(10)使用 -e 选项指定表达式

使用 -e 选项指定表达式,可多次使用。

复制代码
root@dev:~/linux# grep -e "unix" -e "great" -e "os"  text.txt 
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

(11)-f 文件选项 查找符合规则条件的文件内容,格式为每行一个规则样式

复制代码
root@dev:~/linux# cat > pattern.txt
-------------------------------------------------------------------
unix
learn

root@dev:~/linux# grep -f pattern.txt  text.txt 
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
learn operating system.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

(12)打印文件中的 n 个特定行

-A 打印搜索到的行和结果之后的 n 行,-B 打印搜索到的行和结果之前的 n 行,-C 打印搜索到的行和结果之后及之前的 n 行。

语法:

复制代码
$grep -A[NumberOfLines(n)] [search] [file]  

$grep -B[NumberOfLines(n)] [search] [file]  

$grep -C[NumberOfLines(n)] [search] [file]

-A 示例:

复制代码
root@dev:~/linux# grep -A1 great text.txt 
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
learn operating system.

root@dev:~/linux# grep -A2 great text.txt 
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
learn operating system.
Unix linux which one you choose.

-B 示例:

复制代码
root@dev:~/linux# grep -B1 choose text.txt 
-------------------------------------------------------------------
learn operating system.
Unix linux which one you choose.

root@dev:~/linux# grep -B2 choose text.txt 
-------------------------------------------------------------------
unix is great os. unix was developed in Bell labs.
learn operating system.
Unix linux which one you choose.

-C 示例:

复制代码
root@dev:~/linux# grep -C1 choose text.txt 
-------------------------------------------------------------------
learn operating system.
Unix linux which one you choose.
uNix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

(13)目录中递归搜索

-R 指定目录下所有文件中递归搜索。

语法:

复制代码
grep -R [Search] [directory]

示例:

复制代码
root@dev:~/linux# grep -iR operating /root
-------------------------------------------------------------------
Binary file /root/proxy-agent.v0.6.tar matches
/root/get-docker.sh:                    echo "ERROR: Unsupported operating system 'macOS'"
Binary file /root/frontend.tar matches
/root/linux/text.txt:learn operating system.
  • -i 不区分大小写搜索字符串;

  • -R 递归检查目录中的所有文件;

(14)-E 正则表达式

复制代码
root@dev:~/linux# grep -E "^[A-Z][a-z]" text.txt 
-------------------------------------------------------------------
Unix linux which one you choose.

(15)-o 输出文件中匹配到的部分

复制代码
root@dev:~/linux# grep -o -E "[A-Z]" text.txt 
-------------------------------------------------------------------
B
U
N
L
相关推荐
物联网老王1 小时前
Ubuntu Linux Cursor 安装与使用一
linux·运维·ubuntu
一位摩羯座DBA3 小时前
Redhat&Centos挂载镜像
linux·运维·centos
学习3人组3 小时前
CentOS配置网络
linux·网络·centos
weixin_307779133 小时前
Hive集群之间迁移的Linux Shell脚本
大数据·linux·hive·bash·迁移学习
漫步企鹅4 小时前
【蓝牙】Linux Qt4查看已经配对的蓝牙信息
linux·qt·蓝牙·配对
cui_win4 小时前
【网络】Linux 内核优化实战 - net.core.flow_limit_table_len
linux·运维·网络
梦在深巷、4 小时前
MySQL/MariaDB数据库主从复制之基于二进制日志的方式
linux·数据库·mysql·mariadb
冰橙子id5 小时前
linux系统安全
linux·安全·系统安全
stark张宇5 小时前
VMware 虚拟机装 Linux Centos 7.9 保姆级教程(附资源包)
linux·后端
Johny_Zhao5 小时前
Ubuntu系统安装部署Pandawiki智能知识库
linux·mysql·网络安全·信息安全·云计算·shell·yum源·系统运维·itsm·pandawiki