当前位置:首页>Linux>【Linux·基础篇】Shell 基础|常用命令·文本处理三剑客(一):grep

【Linux·基础篇】Shell 基础|常用命令·文本处理三剑客(一):grep

  • 2026-10-11 06:19:08
【Linux·基础篇】Shell 基础|常用命令·文本处理三剑客(一):grep

在 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:忽略大小写

  • • 搜索关键词“hello”,不区分大小写:
    grep -i 'hello' sample.txt

    输出:

    Hello Worldhello worldHELLO WORLD
  • • 搜索关键词“test”,不区分大小写:
    grep -i 'test' sample.txt

    输出:

    This is a test line with ERRORAnother test line with error

选项 -v:反向选择

  • • 显示不包含关键词“test”的行:
    grep -v 'test' sample.txt

    输出:

    Hello Worldhello worldHELLO WORLDWelcome to Linux TutorialNormal operation completeddebug: starting processDEBUG: initializingThis is the end of file
  • • 显示不包含关键词“o”的行:
    grep -v 'o' sample.txt

    输出:

    HELLO WORLDThis is a test line with ERRORDEBUG: initializing
  • • 显示不包含关键词“o”的行,不区分大小写:
    grep -v -i 'o' sample.txt

    输出:

    DEBUG: initializing

选项 -n:显示行号

  • • 搜索关键词“error”,并显示行号:
    grep -n 'error' sample.txt

    输出:

    6:Another test line with error
  • • 搜索关键词“error”,并显示行号,不区分大小写:
    grep -n -i 'error' sample.txt

    输出:

    5:This is a test line with ERROR6:Another test line with error

选项 -c:统计匹配行数

  • • 统计包含关键词“test”的行数:
    grep -c 'test' sample.txt

    输出:

    2
  • • 统计不包含关键词“test”的行数:
    grep -v -c 'test' sample.txt

    输出:

    8

选项 -r:递归搜索

  • • 递归搜索当前目录下所有文件中的“grep”:
    grep -r 'grep' .

    输出:

    ./subdir/file2.txt:another grep example./file1.txt:grep example in file1
  • • 递归搜索当前目录下所有文件中的“another”,不区分大小写:
    grep -r -i 'another' .

    输出:

    ./subdir/file2.txt:another grep example./sample.txt:Another test line with error

选项 -l:只列出文件名

  • • 列出包含关键词“error”的文件名:
    grep -l 'error' *.txt

    输出:

    sample.txt
  • • 递归查找包含关键词“grep”的文件:
    grep -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: initializing
  • • 匹配关键词“hello”或“Hello”:
    grep -E '[Hh]ello' sample.txtegrep '[Hh]ello' sample.txt

    输出:

    Hello Worldhello world

选项 -A num:显示匹配行及其后几行

  • • 显示匹配行及其后 2 行
    grep -A 2 'test' sample.txt

    输出:

    This is a test line with ERRORAnother test line with errorNormal operation completeddebug: starting process

选项 -B num:显示匹配行及其前几行

  • • 显示匹配行及其前 2 行
    grep -B 2 'error' sample.txt

    输出:

    Welcome to Linux TutorialThis is a test line with ERRORAnother test line with error

选项 -C num:显示匹配行及其前后各几行

  • • 显示匹配行及其前后各 1 行(上下文):
    grep -C 1 'Normal' sample.txt

    输出:

    Another test line with errorNormal operation completeddebug: starting process
  • • 搜索关键词“error”(忽略大小写),显示行号和前后各 1 行(上下文):
    grep -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,意味着你获得了一把高效处理文本信息的钥匙。

最新文章

随机文章