Linux操作系统»Linux中的grep命令。 完整指南
Linux grep实用程序用于过滤输出或搜索文件中的特定单词。 这篇文章将介绍Linux中grep实用程序可用的基本选项。
Grep简短格式(全球常规 Ë压缩和打印)。 在基本层面上 grep命令 Linux中的“ Linux”用于使用某种形式的正则表达式来搜索或过滤简单的文本数据。
Linux grep命令和选项
在检查grep命令之前,让我们看一下它的基本语法。
grep命令的基本语法
cat <file name>|grep <string or regular expression> <command>|grep <string or regular expression> grep <string or regular expression> [file name]
grep命令的选项
1.简单的文件搜索
让我们看一下/ etc / passwd文件中的示例,以在文件中查找字符串。 要使用grep查找单词“ system”,请使用以下命令:
[[email protected] ~]# cat /etc/passwd|grep system
输出示例:
systemd-bus-proxy:x:899:897:systemd Bus Proxy:/:/sbin/nologin systemd-network:x:898:896:systemd Network Management:/:/sbin/nologin
2.计算单词的出现。
在上面的示例中,我们在系统中搜索文件中的单词
e“ / etc / passwd”。 如果我们想知道文件中单词的出现次数或次数,请使用以下选项:
[[email protected] ~]# cat /etc/passwd|grep -c system 2 [[email protected] ~]#
上面指出,该单词在文件“ / etc / passwd”中出现了两次。
3.忽略区分大小写的单词
Grep区分大小写,这意味着它将仅搜索文件中的给定单词。 要测试此功能,请创建一个名为“ test.txt”的文件,其内容如下所示:
[[email protected] tmp]# cat test.txt AndreyEx andreyex ANDREYEX Andreyex [[email protected] tmp]#
现在,如果您尝试查找字符串“ grayex”,那么该命令将不会列出具有不同变体的所有单词“ grayex”,如下所示:
[[email protected] tmp]# grep andreyex test.txt andreyex [[email protected] tmp]#
该结果证实仅将显示一个变体,而忽略具有不同变体的单词“ andreyex”的其余部分。 而且,如果您想忽略这种情况,则需要对grep使用“ -i”选项,如下所示:
[[email protected] tmp]# grep -i andreyex test.txt AndreyEx andreyex ANDREYEX Andreyex
4.使用grep命令在文件中的两行不同
现在,如果要使用grep命令搜索两个单词或两行,则必须指定扩展。 在下一个命令中,我们在/ etc / passwd文件中找到两行“ system”和“ nobody”。
[[email protected] ~]# grep 'system|nobody' /etc/passwd nobody:x:89:89:Nobody:/:/sbin/nologin systemd-bus-proxy:x:899:897:systemd Bus Proxy:/:/sbin/nologin systemd-network:x:898:896:systemd Network Management:/:/sbin/nologin [[email protected] ~]#
5.递归搜索
假设您要递归地在目录中的任何位置查找单词或字符串,然后使用-r选项。 例如,如果要在/ etc目录中递归找到单词“ check_oracle”,请使用以下命令:
[[email protected] ~]# grep -r "check_oracle" /etc/ /etc/selinux/targeted/contexts/files/file_contexts:/usr/lib/nagios/plugins/check_oracle -- system_u:object_r:nagios_services_plugin_exec_t:s0 Binary file /etc/selinux/targeted/contexts/files/file_contexts.bin matches /etc/selinux/targeted/modules/active/file_contexts:/usr/lib/nagios/plugins/check_oracle -- system_u:object_r:nagios_services_plugin_exec_t:s0 /etc/selinux/targeted/modules/active/file_contexts.template:/usr/lib/nagios/plugins/check_oracle -- system_u:object_r:nagios_services_plugin_exec_t:s0 [[email protected] ~]
在上面的输出中,我们可以看到在其中找到该行的文件的名称,如果要从最终结果中删除文件名,请使用“ -h”选项,如下所示:
[[email protected] ~]# grep -hr "check_oracle" /etc/ /usr/lib/nagios/plugins/check_oracle -- system_u:object_r:nagios_services_plugin_exec_t:s0 Binary file /etc/selinux/targeted/contexts/files/file_contexts.bin matches /usr/lib/nagios/plugins/check_oracle -- system_u:object_r:nagios_services_plugin_exec_t:s0 /usr/lib/nagios/plugins/check_oracle -- system_u:object_r:nagios_services_plugin_exec_t:s0 [[email protected] ~]#
6. grep命令的输出。
如果要在命令的任何输出中搜索行或单词,则必须在grep中使用“ |”运算符,后跟
[[email protected] ~]# dmesg |grep memory [ 0.000000] Base memory trampoline at [ffff880000098000] 98000 size 19456 [ 0.000000] init_memory_mapping: [mem 0x00000000-0x000fffff] [ 0.000000] init_memory_mapping: [mem 0x3fe00000-0x4fffffff] [ 0.000000] init_memory_mapping: [mem 0x3c000000-0x4fdfffff] [ 0.000000] init_memory_mapping: [mem 0x00100000-0x4bffffff] [ 0.000000] kexec: crashkernel=auto resulted in zero bytes of reserved memory. [ 0.000000] Early memory node ranges [ 0.000000] PM: Registered nosave memory: [mem 0x0003e000-0x0003ffff] [ 0.000000] PM: Registered nosave memory: [mem 0x000a0000-0x000dffff] [ 0.000000] PM: Registered nosave memory: [mem 0x000e0000-0x000fffff] [ 0.000000] please try 'cgroup_disable=memory' option if you don't want memory cgroups [ 0.030181] Initializing cgroup subsys memory [ 0.862358] Freeing initrd memory: 23532k freed [ 1.064599] Non-volatile memory driver v1.3 [ 1.069351] crash memory driver: version 1.1 [ 1.186673] Freeing unused kernel memory: 1430k freed [ 5.567780] [TTM] Zone kernel: Available graphics memory: 480345 kiB [[email protected] ~]#
7.在Linux中使用grep命令反转
假设如果要显示文件中不包含任何特定单词的所有单词,请使用“ -v”选项。 这使您可以创建一个内容如下的文件:
[[email protected] tmp]# cat test.txt Andreyex12 Andreyex454 Andreyex34343 Andreyex LinuxRoutes Linux [[email protected] tmp]#
如果我们不想打印包含Linux一词的行,请使用以下命令。
[[email protected] tmp]# grep -v Linux test.txt Andreyex12 Andreyex454 Andreyex34343 Andreyex
8.精确的单词匹配
根据第7点的示例,如果我们搜索Andreyex,则它将所有出现的Andreyex打印为“ Andreyex12”,“ Andreyex454”,“ Andreyex34343”以及“ Andreyex”,如下所示:
[[email protected] tmp]# grep Andreyex test.txt Andreyex12 Andreyex454 Andreyex34343 Andreyex [[email protected] tmp]#
那么,如果我们想找到确切的单词“ Andreyex”而不是上面列出的所有输出,则使用“ -w”选项,如下所示:
[[email protected] tmp]# grep -w Andreyex test.txt Andreyex [[email protected] tmp]#