Linux下grep与正则表达式

在Linux下,我们经常会使用grep对一些结果进行过过滤,下面简单介绍一个grep和正则表达式结合的用法。
源文件如下:

     1  "Open Source" is a good mechanism to develop programs.$
     2  apple is my favorite food.$
     3  Football game is not use feet only.$
     4  this dress doesn't fit me.$
     5  However, this dress is about $ 3183 dollars.^M$
     6  GNU is free air not free beer.^M$
     7  Her hair is very beauty.^M$
     8  I can't finish the test.^M$
     9  Oh! The soup taste good.^M$
    10  motorcycle is cheap than car.$
    11  This window is clear.$
    12  the symbol '*' is represented as start.$
    13  Oh!^IMy god!$
    14  The gd software is a library for drafting programs.^M$
    15  You are the best is mean you are the no. 1.$
    16  The world  is the same with "glad".$
    17  I like dog.$
    18  google is the best tools for search keyword.$
    19  goooooogle yes!$
    20  go! go! Let's go.$
    21  # I am VBird$
    22  $
  • 查找特定字符串

    • 查找the这个特定字符串:grep -n 'the' regular_express.txt
    Linux下grep与正则表达式_第1张图片
    grep -n 'the' regular_express.txt
    • 查找不包含the这个字符的行:grep -vn 'the' regular_express.txt

      Linux下grep与正则表达式_第2张图片
      grep -vn 'the' regular_express.txt

    • 忽略大小写查找包含the的行:grep -in 'the' regular_express.txt

      Linux下grep与正则表达式_第3张图片
      grep -in 'the' regular_express.txt

  • 利用中括号[]来查找集合字符

    • 比如我想查找test或者taste这两个单词时,可以这么写:grep -n 't[ae]st' regular_express.txt (不论方括号里面有几个字符,它都只代表一个字符

      grep -n 't[ae]st' regular_express.txt

    • 如果不想要oo前面有g的话,可以这么写:grep -n '[^g]oo' regular_express.txt ([^g]中的^表示反向选择,即表示oo前面不能是g)

      grep -n '[^g]oo' regular_express.txt

    • 假如我oo前面不能有小写字符:grep -n '[^a-z]oo' regular_express.txt (a-z就表示小写字符,还有另外一种写法使用特殊字符[:lower:],这个也是表示小写字母-grep -n '[^[:lower:]]oo' regular_express.txt

      grep -n '[^a-z]oo' regular_express.txt

  • 行首和行尾字符^ 和 $

    • 查找到一行中行首为the的:grep -n '^the' regular_express.txt

      grep -n '^the' regular_express.txt

    • 查找不以小写字符开头的:grep -n '[a-z]' regular_express.txt(这里需要注意^ 在[]的里面和外面是两个不同的概念,在[]里面表示反向选择,在[]外面表示定位在行首的意义)

      Linux下grep与正则表达式_第4张图片
      grep -n '^[^a-z]' regular_express.txt

    • 查找行尾是. d的: grep -n '.$' regular_express.txt, 这里有两个点需要注意第一是小数点.之前有一个转移符,这是因为.具有其他意义,因此需要使用转移符\来消除其特殊意义。第二点就是文件中的第5-9行并没有被打印出来。我们如果仔细看源文件的话或发现第5-9行的结尾是.^M$,这里涉及到一个Linux和windows的断行字符的问题,Linux是以LF(\n)断行,而windows是以CRLF(\r\n)断行,所以一般我们在Linux上面查看windows的文件,其结尾都是^M,所以我们在windows下编写的脚本放到Linux上运行之前需要小心,要去除掉这个字符,不然运行会报错。

      Linux下grep与正则表达式_第5张图片
      grep -n '.$' regular_express.txt

你可能感兴趣的:(Linux下grep与正则表达式)