石家庄市住房建设局网站好一点的网站建设公司
一、概念
grep 是 Linux 和 Unix 系统中一个非常常用的命令行工具,用于搜索文本文件中的特定模式。它支持正则表达式,并能在文件中快速查找匹配的行
二、正则表达式
1.概念
正则表达式(Regular Expressions,简称 regex 或 regexp)是一种用于定义字符串模式的强大工具。它可以用来搜索、编辑或处理文本。正则表达式在许多编程语言和工具(如 Python、JavaScript、Grep、Sed)中得到了广泛应用
2.语法
正则表达式由普通字符(如字母、数字)和特殊字符(元字符)组成,用于匹配字符串模式。
-  
普通字符
普通字符包括大多数字母和数字,匹配它们自身。例如,正则表达式 abc 匹配字符串 “abc”。 -  
元字符
元字符是具有特殊含义的字符,用于构建复杂的模式。以下是一些常用的元字符: 
| 元字符 | 描述 | 
|---|---|
| . | 匹配任意单个字符(换行符除外) | 
| ^ | 匹配字符串的开始 | 
| $ | 匹配字符串的结束 | 
| * | 匹配前面的字符零次或多次 | 
| + | 匹配前面的字符一次或多次 | 
| ? | 匹配前面的字符零次或一次 | 
| {n} | 匹配前面的字符恰好 n 次 | 
| {n,} | 匹配前面的字符至少 n 次 | 
| {n,m} | 匹配前面的字符至少 n 次,至多 m 次 | 
| [] | 匹配方括号内的任意字符 | 
| () | 分组,组合多个字符成一个单元,并记住匹配的子字符串 | 
| \ | 转义字符,用于匹配元字符的字面含义 | 
- 字符类
字符类用于匹配一组字符中的任意一个。字符类用方括号表示,例如 [abc] 匹配 “a”、“b” 或 “c”。一些常用的字符类和预定义字符类如下: 
| 字符类 | 描述 | 
|---|---|
| [abc] | 匹配 “a”、“b” 或 “c” | 
| [^abc] | 匹配除 “a”、“b” 和 “c” 之外的任意字符 | 
| [a-z] | 匹配任意小写字母 | 
| [A-Z] | 匹配任意大写字母 | 
| [0-9] | 匹配任意数字 | 
| \d | 匹配任意数字(等价于 [0-9]) | 
| \D | 匹配任意非数字字符 | 
| \w | 匹配任意单词字符(字母、数字或下划线 | 
| \W | 匹配任意非单词字符 | 
| \s | 匹配任意空白字符(空格、制表符、换行符等) | 
| \S | 匹配任意非空白字符 | 
3.常见正则表达式示例:
- 匹配ip地址
(\d{1,3}\.){3}\d{1,3} ## -P Perl 兼容正则表达式 
root@xxx:~# ifconfig  -a  |grep -P "(\d{1,3}\.){3}\d{1,3}"inet addr:10.xx.xx.xx  Bcast:10.xx.xx.xx  Mask:255.255.255.0inet addr:192.168.144.102  Bcast:192.168.144.255  Mask:255.255.255.0inet addr:127.0.0.1  Mask:255.0.0.0
 
([0-9]{1,3}\.){3}[0-9]{1,3} ## -E 兼容扩展正则
root@xxx:~# ip a s  |grep -E  "([0-9]{1,3}\.){3}[0-9]{1,3}"inet 127.0.0.1/8 scope host loinet 192.168.144.102/24 brd 192.168.144.255 scope global eth1inet 10.xx.xx.xx/24 brd 10.xx.xx.xx scope global bond0
 
- 匹配电子邮箱
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z] 
root@xxx:~#  cat .gitconfig  |grep -P "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"
email = xxx@xxx.com
 
三、grep用法
1.基本用法
1)基本语法:
grep [OPTIONS] PATTERN [FILE...]
- PATTERN:要搜索的正则表达式。
 - FILE:要搜索的文件(可以是多个文件)。
 
2)常见选项
- 匹配选项
 
-i 或 --ignore-case:忽略大小写。-v 或 --invert-match:反转匹配,只显示不匹配的行。-w 或 --word-regexp:匹配整个单词。-x 或 --line-regexp:匹配整行。
 
- 输出控制选项
 
-c 或 --count:只输出匹配的行数。-l 或 --files-with-matches:只输出包含匹配的文件名。-L 或 --files-without-match:只输出不包含匹配的文件名。-n 或 --line-number:在输出中显示匹配行的行号。-H 或 --with-filename:在多文件搜索中显示文件名。-h 或 --no-filename:在多文件搜索中不显示文件名。-o 或 --only-matching:只输出匹配的部分。
 
- 上下文控制选项
 
-A NUM 或 --after-context=NUM:显示匹配行后面的 NUM 行。-B NUM 或 --before-context=NUM:显示匹配行前面的 NUM 行。-C NUM 或 --context=NUM:显示匹配行前后各 NUM 行。
 
- 正则表达式选项
 
-E 或 --extended-regexp:使用扩展正则表达式。-F 或 --fixed-strings:将 PATTERN 作为固定字符串搜索。-G 或 --basic-regexp:使用基本正则表达式(默认)。-P 或 --perl-regexp:使用 Perl 兼容正则表达式。
 
- 文件和目录选项
 
-r 或 -R 或 --recursive:递归搜索目录。--exclude=GLOB:排除匹配 GLOB 模式的文件。--include=GLOB:只搜索匹配 GLOB 模式的文件。--exclude-dir=DIR:排除匹配 DIR 名称的目录。
 
2.示例
file.txt
community. Learning
Hello World!
This is a simple
example of a text.
Programmers often
start with "hello, World!"
to test their code.
It's a tradition
in the programming
 
- 基本搜索
 
root@xxx:~# grep "hello" file.txt
start with "hello, World!"
 
在 file.txt 中搜索 “hello”。
- 忽略大小写
 
root@xxx:~# grep -i "hello" file.txt
Hello World!
start with "hello, World!"
 
在 file.txt 中搜索 “hello”,忽略大小写。
- 反转匹配
 
root@xxx:~# grep -v "hello" file.txt
Hello World!
This is a simple
example of a text.
Programmers often
to test their code.
It's a tradition
in the programming
 
显示 file.txt 中不包含 “hello” 的行。
- 显示行号
 
root@xxx:~# grep -n "hello" file.txt
5:start with "hello, World!"
 
在搜索结果中显示行号。
- 递归搜索
 
root@xxx:~#  grep -r "pattern" /path/to/directory
 
递归搜索目录 /path/to/directory 中的所有文件。
- 精确匹配匹配整个单词
 
root@xxx:~#  grep -w "pattern" file.txt
start with "hello, World!"
 
只匹配整个单词 “pattern”。
- 显示匹配行的上下文
 
root@xxx:~#  grep -C 2 "hello" file.txt
example of a text.
Programmers often
start with "hello, World!"
to test their code.
It's a tradition
 
显示匹配行以及前后各两行的内容。
- 使用扩展正则表达式
 
grep -E "pattern1|pattern2" file.txt
 
使用扩展正则表达式,匹配 “pattern1” 或 “pattern2”。
- 只输出匹配的部分
 
grep -o "pattern" file.txt
 
只输出匹配的部分,而不是整行。
- 搜索多个文件
 
grep "pattern" file1.txt file2.txt
 
在 file1.txt 和 file2.txt 中搜索 “pattern”。
- 排除特定文件
 
grep --exclude="*.log" "pattern" /path/to/directory/*
 
在 /path/to/directory 中搜索 “pattern”,但排除扩展名为 .log 的文件。
- 排除特定目录
 
grep --exclude-dir="backup" "pattern" /path/to/directory
 
在 /path/to/directory 中搜索 “pattern”,但排除目录 backup。
