很多时候,我们需要搜索可能存在于多个文件中的特定字符串。在本文中,我们将看到用于查找包含特定字符串或Word的所有文件的命令。
它是一个功能强大的正则表达式搜索工具。在基本级别上,它将使输入字符串与包含该字符串的文件列表匹配。以下是语法和示例。
grep 'string' directory-path/*.* #Example grep 'config' hadoop-2.6.5/etc/hadoop/*.*
运行上面的代码给我们以下结果-
hadoop-2.6.5/etc/hadoop/capacity-scheduler.xml: <configuration> hadoop-2.6.5/etc/hadoop/core-site.xml:<configuration> hadoop-2.6.5/etc/hadoop/hadoop-policy.xml:<configuration> hadoop-2.6.5/etc/hadoop/hdfs-site.xml:<configuration> hadoop-2.6.5/etc/hadoop/httpfs-site.xml:<configuration> hadoop-2.6.5/etc/hadoop/kms-acls.xml:<configuration> hadoop-2.6.5/etc/hadoop/kms-site.xml:<configuration> hadoop-2.6.5/etc/hadoop/mapred-site.xml.template:<configuration> hadoop-2.6.5/etc/hadoop/ssl-client.xml.example:<configuration> hadoop-2.6.5/etc/hadoop/ssl-server.xml.example:<configuration> hadoop-2.6.5/etc/hadoop/yarn-site.xml:<configuration>
在这种情况下,我们提到了r开关,它允许沿着给定路径的所有子目录进行递归搜索。
grep -r 'string' directory-path/ $ grep -r 'done' hadoop-2.6.5/
运行上面的代码给我们以下结果-
hadoop-2.6.5/sbin/slaves.sh:done hadoop-2.6.5/sbin/distribute-exclude.sh:done hadoop-2.6.5/sbin/yarn-daemon.sh:done hadoop-2.6.5/sbin/kms.sh:done hadoop-2.6.5/sbin/httpfs.sh:done hadoop-2.6.5/sbin/refresh-namenodes.sh: done hadoop-2.6.5/sbin/refresh-namenodes.sh: echo "刷新名称节点已完成。" hadoop-2.6.5/sbin/mr-jobhistory-daemon.sh: done hadoop-2.6.5/sbin/hadoop-daemon.sh:done
我们还可以通过将egrep命令与|一起使用来搜索多个单词。字符。在下面的示例中,我们正在搜索包含单词config或逗号的文件。
egrep -r 'word1|word2' directory-path/ #Example egrep -r 'config|comma' hadoop-2.6
运行上面的代码给我们以下结果-
hadoop-2.6.5/etc/hadoop/capacity-scheduler.xml:<configuration> hadoop-2.6.5/etc/hadoop/capacity-scheduler.xml:</configuration> hadoop-2.6.5/etc/hadoop/core-site.xml:&tl;?xml-stylesheet type="text/xsl" href="configuration.xsl"?> hadoop-2.6.5/etc/hadoop/core-site.xml:<configuration> hadoop-2.6.5/etc/hadoop/core-site.xml:</configuration> hadoop-2.6.5/etc/hadoop/hadoop-policy.xml:<?xml-stylesheet type="text/xsl" href="configuration.xsl"?> hadoop-2.6.5/etc/hadoop/hadoop-policy.xml:<configuration> hadoop-2.6.5/etc/hadoop/hadoop-policy.xml: The ACL is a comma-separated list of user and group names. The user and hadoop-2.6.5/etc/hadoop/hadoop-policy.xml: The ACL is a comma-separated list of user and group names. The user and hadoop-2.6.5/etc/hadoop/hadoop-policy.xml: The ACL is a comma-separated list of user and group names. The user and hadoop-2.6.5/etc/hadoop/hadoop-policy.xml: The ACL is a comma-separated list of user and group names. The user and hadoop-2.6.5/etc/hadoop/hadoop-policy.xml: The ACL is a comma-separated list of user and group names. The user and hadoop-2.6.5/etc/hadoop/hadoop-policy.xml: <description>ACL for AdminOperationsProtocol. Used for admin commands. hadoop-2.6.5/etc/hadoop/hadoop-policy.xml: The ACL is a comma-separated list