国外服装图案设计网站,百度虚拟主机怎么使用,wordpress主题开发视频教程,页游网站如何做推广Linux grep命令介绍
grep (Global Regular Expression Print)命令用来在文件中查找包含或者不包含某个字符串的行#xff0c;它是强大的文本搜索工具#xff0c;并可以使用正则表达式进行搜索。当你需要在文件或者多个文件中搜寻特定信息时#xff0c;grep就显得无比重要啦…Linux grep命令介绍
grep (Global Regular Expression Print)命令用来在文件中查找包含或者不包含某个字符串的行它是强大的文本搜索工具并可以使用正则表达式进行搜索。当你需要在文件或者多个文件中搜寻特定信息时grep就显得无比重要啦。
Linux grep命令适用的Linux版本
grep命令在几乎所有的Linux发行版中都可以使用。以下是在CentOS 7和CentOS 8中安装grep的命令。
[linuxbashcommandnotfound.cn ~]$ sudo yum install grep # for CentOS 7
[linuxbashcommandnotfound.cn ~]$ sudo dnf install grep # for CentOS 8Linux grep命令的基本语法
语法格式
grep [options] pattern [file...]Linux grep命令的常用选项或参数说明
参数说明-v–invert-match 反向选择只显示没有匹配到的行-i–ignore-case 忽略大小写-r–recursive 递归处理指定目录下的所有文件以及子目录中的文件-l–files-with-matches 列出文件内容符合指定的样式的文件名称-n–line-number 显示匹配行及其行号–colorauto–color 在显示匹配行时将匹配的字符串以特定颜色突出显示
Linux grep命令实例详解
实例1使用grep查找包含特定字符串的行
使用grep我们可以在文件中查找包含特定字符串的行。这是grep的基本用法。
[linuxbashcommandnotfound.cn ~]$ grep pattern filename实例2使用grep和正则表达式查找字符串
grep不仅能够基于字符串搜寻信息还能够搭配正则表达式进行更为复杂的搜索。
[linuxbashcommandnotfound.cn ~]$ grep regex filename实例3使用grep在多个文件中搜索
grep Command 不仅可以在一个文件中进行搜索也可以在多个文件中查找匹配的行。
[linuxbashcommandnotfound.cn ~]$ grep pattern file1 file2 file3实例4使用grep配合通配符搜索
在某种情况下你可能需要在特定类型的文件如所有的文本(.txt)文件中进行搜索可以使用通配符(*)。
[linuxbashcommandnotfound.cn ~]$ grep pattern *.txt实例5使用grep查找不符合匹配的行
如果你想查找不包含某些字符串或者模式的行可以使用 -v 选项。
[linuxbashcommandnotfound.cn ~]$ grep -v pattern filename实例6使用grep搜索并高亮匹配内容
使用 --colorauto 选项可以高亮显示匹配的字符串。
[linuxbashcommandnotfound.cn ~]$ grep --colorauto pattern filename实例7使用grep读取另一个命令的输出
grep命令可以配合管道操作符(|)搜寻另一个命令的输出。
[linuxbashcommandnotfound.cn ~]$ command | grep pattern实例8使用grep显示匹配字符串的前后行
-c选择项它除了可以列出行号外还可以列出符合范本样式的具体是哪些行假设我们希望找出符合范本样式的前2行那么我们可以这样写
[linuxbashcommandnotfound.cn ~]$ grep -B 2 pattern filename实例9在文件中搜索多个模式
你可以在同一文件中查找多个模式。只需要使用-e选项就可以达到这个目的。
[linuxbashcommandnotfound.cn ~]$ grep -e pattern1 -e pattern2 filename实例10在文本中查找数字
有的时候你可能需要基于握手的数字范围来进行搜索。我们可以结合正则表达式来进行搜索。
[linuxbashcommandnotfound.cn ~]$ grep [0-9] filename实例11在一个目录中查找含有某一字符串的文件
grep指令可以在一个目录中的所有文件中搜寻含有某一指定字符串的文件。
[linuxbashcommandnotfound.cn ~]$ grep -r pattern directory实例12统计文件中匹配某个字符串的行数
使用grep -c我们可以轻易得到文件中匹配特定字符串的行数。
[linuxbashcommandnotfound.cn ~]$ grep -c pattern filename实例13查找特定格式的字符串
有时我们可能需要查找符合特定格式的字符串如我们可以找出所有格式为字母-字母-字母的字符串。
[linuxbashcommandnotfound.cn ~]$ grep [A-Za-z]-[A-Za-z]-[A-Za-z] filename实例14使用grep且忽略大小写
有时候我们对大小写并不敏感可以通过 -i 选项忽略大小写进行查找:
[linuxbashcommandnotfound.cn ~]$ grep -i pattern filename实例15在多级目录中使用grep搜索
使用 -R 或 -r 选项grep 命令可以在多级子目录中进行递归搜索:
[linuxbashcommandnotfound.cn ~]$ grep -R pattern directory实例16显示匹配结果的上下文
有时候我们想知道匹配行的上下文信息即查看它前后的行。可以使用 -A-B-C 选项完成这个需求:
[linuxbashcommandnotfound.cn ~]$ grep -C 5 pattern filename #-A 5显示匹配行之后5行-B 5显示匹配行之前5行实例17显示包含匹配行的文件名
如果你想知道包含匹配行的文件名可以使用 -l 选项:
[linuxbashcommandnotfound.cn ~]$ grep -l pattern file1 file2 file3实例18使用egrep完成多模式搜索
egrep 是 grep 的拓展版它可以同时进行多模式搜索
[linuxbashcommandnotfound.cn ~]$ egrep pattern1|pattern2 filename实例19grep中的正则表达式
grep可以配合正则表达式来使用非常灵活和强大
[linuxbashcommandnotfound.cn ~]$ grep ^pattern filename #搜索以pattern开头的行实例20输出匹配行数量而不是匹配行的内容
如果只想知道匹配行的数量而不是具体的行可以使用 -c 选项
[linuxbashcommandnotfound.cn ~]$ grep -c pattern filenameLinux grep命令的注意事项
如果搜索字符串中包含特殊字符你可能要用引号将搜索字符串括起来grep命令默认只对当前目录下的文件进行递归搜索如果你需要在所有子目录中搜索需要使用-r或者-R选项grep搜索是大小写敏感的如果需要忽略大小写需要使用-i选项。如果你遇到bash: grep: command not found那就按照上述方法进行安装。
Linux grep相关命令
egrep命令扩展grep支持更多的正则表达式fgrep命令速度更快的grep不支持正则表达式sed命令流编辑器用于对文本文件进行复杂的处理awk命令文本和数据处理语言