如何在 Linux 中使用 Grep 命令

GREP代表“全局正则表达式打印”。它是 Linux 提供的一个有用的命令行实用程序,用于搜索与用户提供的模式匹配的文本行。

grep以用户希望在特定文件中搜索的字符串或单词的形式接受用户的输入。然后,该命令检查用户为该模式指定的文件,然后返回与所提供的模式匹配的行。

它通过过滤文件内容来完成出色的工作,从而使我们的任务更容易同时搜索单个或多个文件上的特定内容。

在这篇文章中,让我们回顾一下grep命令并附有一些详细的实际例子。

可用选项grep命令

这些是您将经常使用的一些基本选项grep命令。

选项描述
-i对于不区分大小写的搜索
-r递归搜索指定目录及其子目录中的所有文件
-c显示字符串出现的总次数
-v显示不匹配的行
-w过滤单独使用的特定单词

使用grep命令

grep命令通常与管道(|) 公用事业。当你想与其他一些Linux命令一起使用时,可以使用shell管道来实现。虽然,grep也可以单独使用,无需管道(|) 公用事业。

让我们看看一些基本语法grep带或不带管道实用程序的命令。

首先让我向您展示我将用来说明的示例文本文件grep命令。

INDIA IS A BEAUTIFUL COUNTRY OF PEACE LOVING PEOPLE.
india stands on three pillars of legislature, executive and judiciary.
India Is a Beautiful Country Of Peace Loving People.
India cares for the people as it's resource
cartesian coordinates
importance of all th cartesian coordinates.
Following are two empty lines.



use of a bullock cart is a common sight in the village for the agrarian chores.

This is the end of the sample file.

grep与管道 ( | ) 一起使用 公用事业

grep命令可以与其他 Linux 命令一起使用 shell 管道来实现。就像,使用cat命令显示文件的内容,但同时使用管道输出grep命令仅显示您希望查看的内容。当我们通过这个例子时,这一点会更清楚。

句法:

[command] | grep [string]

例子:

cat sample.txt | grep legislature

在这里,我使用了cat命令显示“sample.txt”文件中的一些行。仅显示其中包含“立法机关”一词的行,并忽略其余行。

输出:

gaurav@ubuntu:~/workspace$ cat sample.txt | grep legislature
india stands on three pillars of legislature, executive and judiciary.
gaurav@ubuntu:~/workspace$

grep不使用管道 ( | ) 公用事业

grep甚至可以直接用作单独的命令,而不使用管道(|) 公用事业。

句法:

grep [string_to_be_searched] [filename]

例子:

grep India sample.txt

输出:

India Is a Beautiful Country Of Peace Loving People.
India cares for the people as it's resource

因此,我使用了grep直接命令过滤文本文件“sample.txt”中包含字符串“India”的行。


不区分大小写的搜索使用grep命令

当我们在终端上执行命令时,Linux 对于区分大小写非常谨慎。这要求用户注意输入命令的字符串的大小写。

让我们通过一个例子来看看这一点。

grep peace sample.txt

在这种情况下,我们不会得到输出,因为示例文件中不存在“peace”一词。我们有一个大写字母“P”的“和平”一词。这个词是相同的,但是当我们使用grep不带任何选项的命令,它会在文件中搜索完全匹配的内容,而忽略字母大小写的任何更改。

为了避免这种歧义,您可以简单地使用-i选项字面意思是grep命令“忘记我放入字符串的情况,只需搜索文件中的所有匹配模式即可。”

句法:

grep -i [string] [filename]

例子:

grep -i peace sample.txt

输出:

INDIA IS A BEAUTIFUL COUNTRY OF PEACE LOVING PEOPLE.
India Is a Beautiful Country Of Peace Loving People.

无论匹配字符串的大小写如何,都会显示所有匹配行。


使用递归搜索grep命令

-r选项将搜索目录及其所有子目录中与用户在命令中提供的字符串模式匹配的所有文件。

句法:

grep -i -r [string] [file_path]

例子:

grep -i -r tomcat /home/gaurav/workspace

这里的字符串是“tomcat”,它将在目录工作区中搜索。 “workspace”目录中的所有子目录和文件也将被扫描以匹配提供的字符串模式。

输出:

./context_log.policy:// catalina.policy - Security Policy Permissions for Tomcat 7
./context_log.policy:// Note: If tomcat-juli.jar is in ${catalina.base} and not in ${catalina.home},
./context_log.policy://  grant codeBase "file:${catalina.base}/bin/tomcat-juli.jar" {..}
./context_log.policy:grant codeBase "file:${catalina.home}/bin/tomcat-juli.jar" {
./context_log.policy:    permission java.lang.RuntimePermission "accessClassInPackage.org.apache.tomcat.websocket.server";
./context.xml:    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
./catalina.properties:# - Tomcat Bootstrap JARs
./catalina.properties:# - Tomcat API JARs
./catalina.properties:# - Tomcat JARs
./catalina.properties:# - Common non-Tomcat JARs
./catalina.properties:org.apache.catalina.startup.TldConfig.jarsToSkip=tomcat7-websocket.jar
./catalina.properties:tomcat.util.buf.StringCache.byte.enabled=true
./catalina.properties:#tomcat.util.buf.StringCache.char.enabled=true
./catalina.properties:#tomcat.util.buf.StringCache.trainThreshold=500000
./catalina.properties:#tomcat.util.buf.StringCache.cacheSize=5000
./server.xml:              pathname="conf/tomcat-users.xml" />
./server.xml:    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
./server.xml:    <Connector executor="tomcatThreadPool"
./server.xml:         every request.  The Engine implementation for Tomcat stand alone
./tomcat-users.xml:<tomcat-users>
./tomcat-users.xml:  <user username="role1" password="tomcat" roles="role1"/>
./tomcat-users.xml:</tomcat-users>
./web.xml:  <!-- loaded into this instance of Tomcat.  As each application is         -->
./web.xml:  <!-- used by Tomcat to support JSP pages.  Traditionally, this servlet    -->

笔记: 使用时-r选项与grep命令我们需要提供文件的路径而不是文件名


仅搜索整个单词grep命令

很多时候,您将搜索一个单词,但最终会使用匹配行填充您的终端,这些匹配行确实包含您的匹配单词,但不是作为单个单词。您可能会看到包含一些单词的行,其子部分是您输入的字符串。

对此感到困惑吗?别担心,一旦你明白了这个例子,就更容易理解了。

例子:

在这里,我想搜索单个单词“cart”并显示文件“sample.txt”中与该单词匹配的所有行。

grep -i cart sample.txt

输出:

Cartesian coordinates
importance of all the Cartesian coordinates.
use of a bullock cart is a common sight in the village for the agrarian chores
The cart went missing as the boy left it loose.

在输出中,您可以观察到单词“Cartesian”也包含单词“cart”,因此,即使我们不希望显示包含单词“Cartesian”的行,也会显示它们。

您可以使用-w选项与grep命令来解决这个歧义。

句法:

grep -i -w [string] [filename]

例子:

grep -i -w cart sample.txt

输出:

use of a bullock cart is a common sight in the village for the agrarian chores.
The cart went missing as the boy left it loose.

现在,当您使用了 –w选项与grep您只会得到“cart”一词作为一个整体使用的行。


反向搜索使用grep命令

grep命令也可以以相反的方式使用。我们可以使用grep命令相反,隐藏匹配的行并仅显示未找到匹配的行。您可以使用-v选项与grep命令。

句法:

grep -i -v [string] [filename]

例子:

grep -i -v resource sample.txt

输出:

INDIA IS A BEAUTIFUL COUNTRY OF PEACE LOVING PEOPLE.
india stands on three pillars of legislature, executive and judiciary.
India Is a Beautiful Country Of Peace Loving People.
cartesian coordinates
importance of all th cartesian coordinates.




use of a bullock cart is a common sight in the village for the agrarian chores.
This is the end of the sample file.

在输出中,除了包含单词“resource”的行之外,所有其他行都会显示。


计算匹配字符串的出现次数

的输出grep如果文件中的数据很大,命令通常会很长。匹配的次数越多,输出的时间就越长grep命令。 Linux 为您提供了一个选项,您可以在其中显示匹配项出现的次数。

句法:

grep -i -c [string] [filename]

例子:

grep -i -c india sample.txt

输出:

gaurav@ubuntu:~/workspace$ grep -i -c india sample.txt
4
gaurav@ubuntu:~/workspace$

这里,输出是一个数字,它是文件sample.txt中单词“India”出现的次数。

笔记: 我用过-i每个示例中的选项都是为了安全地解决区分大小写问题。如果您确定要搜索的单词的大小写,则可以安全地省略-i选项。


结论

我们已经了解了它的基本用途grep本教程中 Linux 系统上的命令。我们还学会了显示最适合我们要求的各种内容,而不是让终端挤满线路。grep如果用于扫描大型数据集,命令肯定会节省时间。