GREP

七. grep家族:1.  grep退出状态:0: 表示成功;1: 表示在所提供的文件无法找到匹配的pattern;2: 表示参数中提供的文件不存在。见如下示例:/> grep 'root' /etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin/> echo $?0/> grep 'root1' /etc/passwd  #用户root1并不存在/> echo $?1/> grep 'root' /etc/passwd1  #这里的/etc/passwd1文件并不存在grep: /etc/passwd1: No such file or directory/> echo $?22.  grep中应用正则表达式的实例:需要说明的是下面所涉及的正则表达式在上一篇中已经给出了详细的说明,因此在看下面例子的时候,可以与前一篇的正则说明部分结合着看。/> cat testfilenorthwest        NW      Charles Main           3.0     .98     3       34western           WE       Sharon Gray          5.3     .97     5       23southwest       SW       Lewis Dalsass         2.7     .8       2       18southern         SO       Suan Chin               5.1     .95     4       15southeast       SE        Patricia Hemenway    4.0     .7       4       17eastern           EA        TB Savage              4.4     .84     5       20northeast        NE        AM Main Jr.              5.1     .94     3       13north              NO       Margot Weber          4.5     .89     5       9central            CT        Ann Stephens          5.7     .94     5       13/> grep NW testfile     #打印出testfile中所有包含NW的行。northwest       NW      Charles Main        3.0     .98     3       34/> grep '^n' testfile   #打印出以n开头的行。northwest       NW      Charles Main        3.0     .98     3       34northeast        NE       AM Main Jr.          5.1     .94     3       13north              NO      Margot Weber      4.5     .89     5       9/> grep '4$' testfile   #打印出以4结尾的行。northwest       NW      Charles Main        3.0     .98     3       34/> grep '5\..' testfile #打印出第一个字符是5,后面跟着一个.字符,在后面是任意字符的行。western         WE      Sharon Gray         5.3     .97     5       23southern        SO      Suan Chin             5.1     .95     4       15northeast       NE      AM Main Jr.            5.1     .94     3       13central           CT      Ann Stephens        5.7     .94     5       13/> grep '\.5' testfile  #打印出所有包含.5的行。north           NO      Margot Weber        4.5     .89     5       9/> grep '^[we]' testfile #打印出所有以w或e开头的行。western         WE      Sharon Gray         5.3     .97     5       23eastern          EA      TB Savage            4.4     .84     5       20/> grep '[^0-9]' testfile #打印出所有不是以0-9开头的行。northwest       NW     Charles Main             3.0     .98      3       34western          WE      Sharon Gray             5.3     .97     5       23southwest       SW     Lewis Dalsass           2.7     .8       2       18southern         SO      Suan Chin                5.1     .95     4       15southeast        SE      Patricia Hemenway     4.0     .7      4       17eastern           EA      TB Savage                4.4     .84     5       20northeast        NE      AM Main Jr.                5.1     .94     3       13north              NO      Margot Weber           4.5     .89     5       9central            CT      Ann Stephens            5.7     .94     5       13/> grep '[A-Z][A-Z] [A-Z]' testfile #打印出所有包含前两个字符是大写字符,后面紧跟一个空格及一个大写字母的行。eastern          EA      TB Savage       4.4     .84     5       20northeast       NE      AM Main Jr.      5.1     .94     3       13注:在执行以上命令时,如果不能得到预期的结果,即grep忽略了大小写,导致这一问题的原因很可能是当前环境的本地化的设置问题。对于以上命令,如果我将当前语言设置为en_US的时候,它会打印出所有的行,当我将其修改为中文环境时,就能得到我现在的输出了。/> export LANG=zh_CN  #设置当前的语言环境为中文。/> export LANG=en_US  #设置当前的语言环境为美国。/> export LANG=en_Br  #设置当前的语言环境为英国。/> grep '[a-z]\{9\}' testfile #打印所有包含每个字符串至少有9个连续小写字符的字符串的行。northwest        NW      Charles Main          3.0     .98     3       34southwest       SW      Lewis Dalsass         2.7     .8       2       18southeast        SE      Patricia Hemenway   4.0     .7       4       17northeast        NE      AM Main Jr.              5.1     .94     3       13#第一个字符是3,紧跟着一个句点,然后是任意一个数字,然后是任意个任意字符,然后又是一个3,然后是制表符,然后又是一个3,需要说明的是,下面正则中的\1表示\(3\)。/> grep '\(3\)\.[0-9].*\1    *\1' testfilenorthwest       NW      Charles Main        3.0     .98     3       34/> grep '\<north' testfile    #打印所有以north开头的单词的行。northwest       NW      Charles Main          3.0     .98     3       34northeast        NE       AM Main Jr.            5.1     .94     3       13north              NO      Margot Weber        4.5     .89     5       9/> grep '\<north\>' testfile  #打印所有包含单词north的行。north           NO      Margot Weber        4.5     .89     5       9/> grep '^n\w*' testfile      #第一个字符是n,后面是任意字母或者数字。northwest       NW     Charles Main          3.0     .98     3       34northeast        NE      AM Main Jr.            5.1     .94     3       13north             NO      Margot Weber        4.5     .89     5       93.  扩展grep(grep -E 或者 egrep):使用扩展grep的主要好处是增加了额外的正则表达式元字符集。下面我们还是继续使用实例来演示扩展grep。/> egrep 'NW|EA' testfile     #打印所有包含NW或EA的行。如果不是使用egrep,而是grep,将不会有结果查出。northwest       NW      Charles Main        3.0     .98     3       34eastern         EA      TB Savage           4.4     .84     5       20/> grep 'NW\|EA' testfile     #对于标准grep,如果在扩展元字符前面加\,grep会自动启用扩展选项-E。northwest       NW      Charles Main        3.0     .98     3       34eastern           EA       TB Savage           4.4     .84     5       20/> egrep '3+' testfile/> grep -E '3+' testfile/> grep '3\+' testfile        #这3条命令将会打印出相同的结果,即所有包含一个或多个3的行。northwest       NW      Charles Main         3.0     .98     3       34western          WE      Sharon Gray         5.3     .97     5       23northeast        NE       AM Main Jr.           5.1     .94     3       13central            CT       Ann Stephens       5.7     .94     5       13/> egrep '2\.?[0-9]' testfile/> grep -E '2\.?[0-9]' testfile/> grep '2\.\?[0-9]' testfile #首先含有2字符,其后紧跟着0个或1个点,后面再是0和9之间的数字。western         WE       Sharon Gray          5.3     .97     5       23southwest      SW      Lewis Dalsass         2.7     .8      2       18eastern          EA       TB Savage             4.4     .84     5       20/> egrep '(no)+' testfile/> grep -E '(no)+' testfile/> grep '\(no\)\+' testfile   #3个命令返回相同结果,即打印一个或者多个连续的no的行。northwest       NW      Charles Main        3.0     .98     3       34northeast        NE       AM Main Jr.          5.1     .94     3       13north              NO      Margot Weber      4.5     .89     5       9/> grep -E '\w+\W+[ABC]' testfile #首先是一个或者多个字母,紧跟着一个或者多个非字母数字,最后一个是ABC中的一个。northwest       NW     Charles Main       3.0     .98     3       34southern        SO      Suan Chin           5.1     .95     4       15northeast       NE      AM Main Jr.          5.1     .94     3       13central           CT      Ann Stephens      5.7     .94     5       13/> egrep '[Ss](h|u)' testfile/> grep -E '[Ss](h|u)' testfile/> grep '[Ss]\(h\|u\)' testfile   #3个命令返回相同结果,即以S或s开头,紧跟着h或者u的行。western         WE      Sharon Gray       5.3     .97     5       23southern        SO      Suan Chin          5.1     .95     4       15/> egrep 'w(es)t.*\1' testfile    #west开头,其中es为\1的值,后面紧跟着任意数量的任意字符,最后还有一个es出现在该行。northwest       NW      Charles Main        3.0     .98     3       344.  grep选项:这里先列出grep常用的命令行选项:选项说明-c只显示有多少行匹配,而不具体显示匹配的行。-h不显示文件名。-i在字符串比较的时候忽略大小写。-l只显示包含匹配模板的行的文件名清单。-L只显示不包含匹配模板的行的文件名清单。-n在每一行前面打印改行在文件中的行数。-v反向检索,只显示不匹配的行。-w只显示完整单词的匹配。-x只显示完整行的匹配。-r/-R如果文件参数是目录,该选项将递归搜索该目录下的所有子目录和文件。/> grep -n '^south' testfile  #-n选项在每一个匹配行的前面打印行号。3:southwest     SW      Lewis Dalsass         2.7     .8      2       184:southern       SO      Suan Chin               5.1     .95     4       155:southeast      SE      Patricia Hemenway    4.0     .7      4       17/> grep -i 'pat' testfile     #-i选项关闭了大小写敏感。southeast       SE      Patricia Hemenway       4.0     .7      4       17/> grep -v 'Suan Chin' testfile #打印所有不包含Suan Chin的行。northwest       NW      Charles Main          3.0     .98     3       34western          WE      Sharon Gray           5.3     .97    5       23southwest       SW      Lewis Dalsass        2.7     .8      2       18southeast        SE      Patricia Hemenway   4.0     .7      4       17eastern           EA      TB Savage              4.4     .84     5       20northeast        NE      AM Main Jr.             5.1     .94     3       13north              NO      Margot Weber        4.5     .89     5       9central            CT      Ann Stephens         5.7     .94     5       13/> grep -l 'ss' testfile  #-l使得grep只打印匹配的文件名,而不打印匹配的行。testfile/> grep -c 'west' testfile #-c使得grep只打印有多少匹配模板的行。3/> grep -w 'north' testfile #-w只打印整个单词匹配的行。north           NO      Margot Weber    4.5     .89     5       9/> grep -C 2 Patricia testfile #打印匹配行及其上下各两行。southwest      SW     Lewis Dalsass         2.7     .8       2       18southern        SO      Suan Chin              5.1     .95     4       15southeast       SE      Patricia Hemenway   4.0     .7      4       17eastern          EA      TB Savage              4.4     .84     5       20northeast       NE      AM Main Jr.             5.1     .94     3       13/> grep -B 2 Patricia testfile #打印匹配行及其前两行。southwest      SW      Lewis Dalsass         2.7     .8      2       18southern        SO      Suan Chin               5.1     .95    4       15southeast       SE      Patricia Hemenway   4.0     .7      4       17/> grep -A 2 Patricia testfile #打印匹配行及其后两行。southeast       SE      Patricia Hemenway   4.0     .7      4       17eastern           EA      TB Savage              4.4     .84     5       20northeast       NE       AM Main Jr.             5.1     .94     3       13

(0)

相关推荐

  • grep 命令搜索 带空格的字符

    grep - n ' a[[:space:]]b' 就能搜索到 'a    b'类似的字符了 grep "^#[:space:]*" /etc/inittab 这个命令中" ...

  • grep匹配回车符的问题

    对于不识别CRLF格式文本文件的grep命令(比如Linux和Cygwin下面的grep)来说,回车符(carriage return)\r 并不是有特殊含义的字符,而是普通字符,所以如果要匹配回车符 ...

  • 【竺】Linux笔记3——ps -ef|grep详解

    ps命令将某个进程显示出来 grep命令是查找 中间的|是管道命令 是指ps命令与grep同时执行 PS是LINUX下最常用的也是非常强大的进程查看命令 grep命令是查找,是一种强大的文本搜索工具, ...

  • Linux用ls和grep统计文件个数

    Linux ls命令 Linux ls命令用于显示指定工作目录下之内容(列出目前工作目录所含之文件及子目录). 语法 ls [-alrtAFR] [name...] 参数 : -a 显示所有文件及目录 ...

  • grep命令有哪些参数选项?linux系统运维

    grep命令是Linux系统中重要的命令之一.grep命令的强大之处主要在于与正则表达式以及扩展的正则表达式及egrep命令的配合.grep命令的功能在于从文本文件或管道数据流中筛选匹配的行及数据.如 ...

  • 通过测试 grep 插件,一起了解验证插件功能的方法

    " 本文是 filter_grep 过滤插件的使用示例,同时也演示了测试插件功能的一般性方法." Fluentd 以插件丰富著称,我们不可能了解和使用所有的插件. 在使用一个新插件 ...

  • Fluentd 过滤插件:grep 用法详解

    " filter_grep 是一个常用的过滤日志内容的插件." 熟悉或者使用过 Linux 系统的小伙伴应该知道,Linux 中有三个处理文本内容的利器:grep.awk 和 se ...

  • ubuntu 如何使用 GREP 命令提取 GREP 行的下一行

    我有文本文件(整个文件的特定部分): ! lat long elev (m) ! 44.5199005 11.6468000 50.4276 我需要提取第二行并将它们保存在另一个文本文件中.数字是可变 ...

  • linux history|grep 操作

    linux 下查询之前用过的命令. history|grep amass history|grep ls history|grep "ls -a"

  • Linux 中强大且常用命令:find、grep

    http://www.shengchulai.com/blog-YBJEiyFQfx.htm 在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find.grep命令,他哥俩可以算是必会 ...