Linux之grep命令详解
- 1. 基本语法
- 2. 常用选项
- 2.1 `-i`:忽略大小写
- 2.2 `-v`:反向匹配
- 2.3 `-r` 或 `-R`:递归搜索目录
- 2.4 `-n`:显示行号
- 2.5 `-c`:只显示匹配的行数
- 2.6 `-l` 和 `-L`:只显示文件名
- 2.7 `-w`:匹配整个单词
- 2.8 `-x`:匹配整行
- 2.9 `-A`、`-B`、`-C`:显示上下文行
- 2.10 `-E`:使用扩展正则表达式
- 2.11 `-f`:从文件中读取模式
- 2.12 `-o`:仅输出匹配部分
- 3. `grep` 正则表达式示例
- 4. 实用示例
- 4.1 在系统日志中查找错误信息
- 4.2 查找包含多个关键字的行
- 4.3 忽略二进制文件搜索文本
- 4.4 从命令输出中查找特定行
- 5. 总结
grep 是 Linux 中用于搜索文本的强大命令,它能根据给定的模式在文件或标准输入中查找匹配的行,并将包含匹配模式的行输出。
1. 基本语法
grep [选项] 搜索模式 文件搜索模式:可以是一个字符串或正则表达式,用来匹配文件中的内容。
文件:指定要搜索的文件或路径。
示例
[root@test test]# cat test.txt
ni hao lisi
hello zhangsan
wo shi wangwu
hello zhaoliu
[root@test test]# grep "hello" test.txt
hello zhangsan
hello zhaoliu
2. 常用选项
2.1 -i
:忽略大小写
忽略大小写来匹配文本。
[root@test test]# cat test.txt
ni hao lisi
hello zhangsan
wo shi wangwu
hello zhaoliu
HELLO aaaaa
bbbb HelLo
[root@test test]# grep "hello" test.txt
hello zhangsan
hello zhaoliu
[root@test test]# grep -i "hello" test.txt
hello zhangsan
hello zhaoliu
HELLO aaaaa
bbbb HelLo
2.2 -v
:反向匹配
只显示不匹配搜索模式的行。
[root@test test]# cat test.txt
ni hao lisi
hello zhangsan
wo shi wangwu
hello zhaoliu
HELLO aaaaa
bbbb HelLo[root@test test]# grep -v "hello" test.txt
ni hao lisi
wo shi wangwu
HELLO aaaaa
bbbb HelLo
2.3 -r
或 -R
:递归搜索目录
在目录及其子目录中递归地搜索匹配项。
[root@test test]# grep -r "hello" /root/test/
/root/test/test.txt:hello zhangsan
/root/test/test.txt:hello zhaoliu
2.4 -n
:显示行号
显示匹配行在文件中的行号。
[root@test test]# grep -n "hello" test.txt
2:hello zhangsan
4:hello zhaoliu
2.5 -c
:只显示匹配的行数
显示文件中匹配模式的行数,而不是具体内容。
[root@test test]# grep -c "hello" test.txt
2
2.6 -l
和 -L
:只显示文件名
-l
:显示包含匹配模式的文件名。-L
:显示不包含匹配模式的文件名。
[root@test test]# grep -l "hello" *.txt
test.txt
2.7 -w
:匹配整个单词
只匹配完整的单词,而不是单词的一部分。
[root@test test]# grep -w "hello" test.txt
hello zhangsan
hello zhaoliu
2.8 -x
:匹配整行
只有当整行匹配搜索模式时才显示该行。
[root@test test]# grep -x "hello" test.txt
[root@test test]# grep -x "hello zhaoliu" test.txt
hello zhaoliu
2.9 -A
、-B
、-C
:显示上下文行
-A n
:显示匹配行之后的 n 行。-B n
:显示匹配行之前的 n 行。-C n
:显示匹配行前后各 n 行。
[root@test test]# grep -A 2 "hello" test.txt # 匹配行和之后2行
hello zhangsan
wo shi wangwu
hello zhaoliu
HELLO aaaaa
bbbb HelLo[root@test test]# grep -B 2 "hello" test.txt # 匹配行和之前2行
ni hao lisi
hello zhangsan
wo shi wangwu
hello zhaoliu[root@test test]# grep -C 2 "hello" test.txt # 匹配行和前后各2行
ni hao lisi
hello zhangsan
wo shi wangwu
hello zhaoliu
HELLO aaaaa
bbbb HelLo
2.10 -E
:使用扩展正则表达式
启用扩展正则表达式(等同于 egrep
),支持更多正则表达式操作符,如 |
, +
, ?
, {}
等。
grep -E "hello|world" file.txt
2.11 -f
:从文件中读取模式
从指定文件中读取多行模式进行搜索。
grep -f pattern.txt file.txt
在 file.txt
中查找 pattern.txt
文件中的每个模式。
2.12 -o
:仅输出匹配部分
仅输出匹配的部分,而不是整行。
[root@test test]# grep -o "hello" test.txt
hello
hello
3. grep
正则表达式示例
-
匹配开头的字符串
^
:grep "^hello" file.txt
匹配以
"hello"
开头的行。 -
匹配行尾的字符串
$
:grep "world$" file.txt
匹配以
"world"
结尾的行。 -
匹配任意单字符
.
:grep "h.llo" file.txt
匹配
"hallo"
、"hello"
等。 -
匹配多个字符
*
:grep "hel*o" file.txt
匹配
"heo"
、"hello"
、"hellllo"
等。 -
匹配字符集
[]
:grep "h[aeiou]llo" file.txt
匹配
"hallo"
、"hello"
、"hollo"
等。 -
匹配字符范围
[a-z]
:grep "h[a-z]llo" file.txt
匹配
"hallo"
、"hello"
等小写字符的组合。 -
匹配多个模式
|
(需配合-E
或egrep
使用):grep -E "hello|world" file.txt
匹配包含
"hello"
或"world"
的行。
4. 实用示例
4.1 在系统日志中查找错误信息
[root@test test]# grep -i "error" /var/log/messages
Nov 10 15:04:18 test kernel: BERT: Boot Error Record Table support is disabled. Enable it by using bert_enable as kernel parameter.
Nov 10 15:04:25 test mcelog: ERROR: AMD Processor family 23: mcelog does not support this processor. Please use the edac_mce_amd module instead.
4.2 查找包含多个关键字的行
[root@test test]# grep -i "error" /var/log/messages |grep "Unable"
Nov 10 15:04:27 test libvirtd: 2024-11-10 07:04:27.035+0000: 1432: error : virHostCPUGetTscInfo:1389 : Unable to open /dev/kvm: No such file or directory
Nov 10 15:04:27 test libvirtd: 2024-11-10 07:04:27.173+0000: 1432: error : virHostCPUGetTscInfo:1389 : Unable to open /dev/kvm: No such file or directory
Nov 10 15:04:27 test libvirtd: 2024-11-10 07:04:27.174+0000: 1432: error : virHostCPUGetTscInfo:1389 : Unable to open /dev/kvm: No such file or directory
4.3 忽略二进制文件搜索文本
grep -I "hello" *
4.4 从命令输出中查找特定行
[root@test test]# dmesg |grep "usb"
[ 0.678613] usbcore: registered new interface driver usbfs
[ 0.678618] usbcore: registered new interface driver hub
[ 0.678720] usbcore: registered new device driver usb
5. 总结
grep
是文本搜索和处理的利器,结合各种选项和正则表达式,可以满足多样化的搜索需求,特别适合日志分析和数据处理等场景。