在 Linux 系统中,“文本处理三剑客”指的是三个核心命令行工具:
grep、sed和awk。它们各自分工明确,常配合使用,构成高效处理文本数据的基础能力,是每个运维人员的必备工具。本文将详细介绍 Linux 文本处理三剑客之首——grep命令。
[本文基于 CentOS Linux release 7 系统环境]
在 Linux 的世界里,我们每天都在与文本打交道:查看日志、分析数据、检查配置……如何从浩如烟海的文本中,快速、精确地找到我们需要的那一行或那一个关键词?这时,我们就需要一位强大的文本搜索与过滤工具——grep 命令。
grep(Global Regular Expression Print)- 打印匹配指定模式的行。grep [OPTION]... PATTERN [FILE]...,即 grep [选项]... 匹配模式 [文件]...grep 是一个强大的文本搜索工具。grep 会读取给定的文件(一份或多份)或标准输入,找到所有包含“匹配模式(字符串或正则表达式)”的所有行,并将这些行打印到标准输出。其强大之处在于“匹配模式”支持正则表达式,并且拥有众多实用选项来实现不同搜索需求。其中,“PATTERN(匹配模式)”默认是一个基本正则表达式(缩写为“BRE”)。
简单来说,grep是用来筛选内容的。
-i:忽略大小写。-v:反向选择,即打印不包含匹配模式的行。-n:显示匹配行及其在原文件中的行号。-c:只统计匹配到的行数,而非内容。-r:递归搜索指定目录下的所有文件。-l:只列出包含匹配模式的文件名,而非具体行。-E:启用扩展正则表达式(缩写为“ERE”,功能更强大),等同于 egrep。-A num:显示匹配行及其后面的 num 行。-B num:显示匹配行及其前面的 num 行。-C num:显示匹配行及其前后各num 行。【注】有关正则表达式的内容,可参考往期文章:
其中,有关基本正则表达式(BRE)和扩展正则表达式(ERE)的内容,我会补充在后续文章里,此处暂不做探讨。
为了方便演示 grep 命令每个常用选项的使用示例,我们需要提前准备如下的测试环境。
test_dir/├── file1.txt├── file3.txt├── sample.txt└── subdir └── file2.txt现在,让我们开始操作吧——很简单,就是在终端上输入一些之前就了解过的命令!
首先,创建测试目录结构:
mkdir -p test_dir/subdir其次,创建示例文件 sample.txt:
cat > test_dir/sample.txt << 'EOF'Hello Worldhello worldHELLO WORLDWelcome to Linux TutorialThis is a test line with ERRORAnother test line with errorNormal operation completeddebug: starting processDEBUG: initializingThis is the end of fileEOF再次,创建其它的示例文件:
echo "grep example in file1" > test_dir/file1.txtecho "another grep example" > test_dir/subdir/file2.txtecho "no match here" > test_dir/file3.txt最后,进入测试目录 test_dir。
cd test_dir/【注】通过往期文章:
相信大家对以上命令已经不再陌生,此处就不再赘述了。
至此,测试环境已经准备好了。接下来,就让我们尝试使用 grep 命令的常用选项吧。
-i:忽略大小写grep -i 'hello' sample.txt输出:
Hello Worldhello worldHELLO WORLDgrep -i 'test' sample.txt输出:
This is a test line with ERRORAnother test line with error-v:反向选择grep -v 'test' sample.txt输出:
Hello Worldhello worldHELLO WORLDWelcome to Linux TutorialNormal operation completeddebug: starting processDEBUG: initializingThis is the end of filegrep -v 'o' sample.txt输出:
HELLO WORLDThis is a test line with ERRORDEBUG: initializinggrep -v -i 'o' sample.txt输出:
DEBUG: initializing-n:显示行号grep -n 'error' sample.txt输出:
6:Another test line with errorgrep -n -i 'error' sample.txt输出:
5:This is a test line with ERROR6:Another test line with error-c:统计匹配行数grep -c 'test' sample.txt输出:
2grep -v -c 'test' sample.txt输出:
8-r:递归搜索grep -r 'grep' .输出:
./subdir/file2.txt:another grep example./file1.txt:grep example in file1grep -r -i 'another' .输出:
./subdir/file2.txt:another grep example./sample.txt:Another test line with error-l:只列出文件名grep -l 'error' *.txt输出:
sample.txtgrep -rl 'grep' .输出:
./subdir/file2.txt./file1.txt-E:扩展正则表达式grep -E 'error|ERROR|debug|DEBUG' sample.txtegrep 'error|ERROR|debug|DEBUG' sample.txt输出:
This is a test line with ERRORAnother test line with errordebug: starting processDEBUG: initializinggrep -E '[Hh]ello' sample.txtegrep '[Hh]ello' sample.txt输出:
Hello Worldhello world-A num:显示匹配行及其后几行grep -A 2 'test' sample.txt输出:
This is a test line with ERRORAnother test line with errorNormal operation completeddebug: starting process-B num:显示匹配行及其前几行grep -B 2 'error' sample.txt输出:
Welcome to Linux TutorialThis is a test line with ERRORAnother test line with error-C num:显示匹配行及其前后各几行grep -C 1 'Normal' sample.txt输出:
Another test line with errorNormal operation completeddebug: starting processgrep -i -n -C 1 'error' sample.txt输出:
4-Welcome to Linux Tutorial5:This is a test line with ERROR6:Another test line with error7-Normal operation completed【注】在实际使用 grep 命令时,可以根据具体需求灵活地组合这些选项。
通过以上介绍,我们看到 grep 绝非一个简单的查找命令。从基础的字符串匹配,到利用正则表达式进行复杂模式搜索,再到结合各种选项实现精确过滤和上下文查看,grep 是每位 Linux 用户命令工具箱中最常用、最核心的组件之一。熟练掌握 grep,意味着你获得了一把高效处理文本信息的钥匙。