学习啦>学习电脑>操作系统>Linux教程>

linux中的grep命令的用法详解(2)

时间: 佳洲1085 分享

  查找非小写字母加oo的内容

  [root@ www.linuxidc.com]# grep -n '[^a-z]oo' regular_express.txt

  3:Football game is not use feet only.

  获取有数字的一行

  [root@ www.linuxidc.com]# grep -n '[0-9]' regular_express.txt

  5:However, this dress is about $ 3183 dollars.

  15:You are the best is mean you are the no. 1.

  [root@ www.linuxidc.com]# grep -n '[^[:lower:]]oo' regular_express.txt

  3:Football game is not use feet only.

  查询以the开头的字符

  [root@ www.linuxidc.com]# grep -n '^the' regular_express.txt

  12:the symbol '*' is represented as start.

  查询开头是小写字母的内容

  [root@ www.linuxidc.com]# grep -n '^[a-z]' regular_express.txt

  2:apple is my favorite food.

  4:this dress doesn't fit me.

  10:motorcycle is cheap than car.

  12:the symbol '*' is represented as start.

  18:google is the best tools for search keyword.

  19:goooooogle yes!

  20:go! go! Let's go.

  查询第一个字符不是大写的

  [root@ www.linuxidc.com]# grep -n '^[[:lower:]]' regular_express.txt

  2:apple is my favorite food.

  4:this dress doesn't fit me.

  10:motorcycle is cheap than car.

  12:the symbol '*' is represented as start.

  18:google is the best tools for search keyword.

  19:goooooogle yes!

  20:go! go! Let's go.

  查询不是以英文开头的字符

  [root@ www.linuxidc.com]# grep -n '^[^a-zA-Z]' regular_express.txt

  1:"Open Source" is a good mechanism to develop programs.

  21:# I am VBird

  查询结尾是小数点的行的内容.

  [root@ www.linuxidc.com]# grep -n '\.$' regular_express.txt

  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.

  10:motorcycle is cheap than car.

  11:This window is clear.

  12:the symbol '*' is represented as start.

  15:You are the best is mean you are the no. 1.

  16:The world <Happy> is the same with "glad".

  17:I like dog.

  18:google is the best tools for search keyword.

  20:go! go! Let's go.

  查找“空白行”

  [root@ www.linuxidc.com]# grep -n '^$' regular_express.txt

  22:

  通配符.和*的使用,.(小数点)代表一定有一个任意字符,*代表重复前一个到无穷多次的意思

  [root@ www.linuxidc.com]# grep -n 'g..d' regular_express.txt

  1:"Open Source" is a good mechanism to develop programs.

  9:Oh! The soup taste good.

  16:The world <Happy> is the same with "glad".

  查询出现任意数字的行列

  [root@ www.linuxidc.com]# grep -n '[0-9][0-9]*' regular_express.txt

  5:However, this dress is about $ 3183 dollars.

  15:You are the best is mean you are the no. 1.

  查询出现两个o的字符串

  [root@ www.linuxidc.com]# grep -n 'o\{2\}' regular_express.txt

  1:"Open Source" is a good mechanism to develop programs.

  2:apple is my favorite food.

  3:Football game is not use feet only.

  9:Oh! The soup taste good.

  18:google is the best tools for search keyword.

  19:goooooogle yes!

  查询出现2-5个o的字符串,后面在接一个g的字符串

  [root@ www.linuxidc.com]# grep -n 'o\{2,5\}g' regular_express.txt

  18:google is the best tools for search keyword.

  19:goooooogle yes!

  查询出现2个以上o的字符串,后面在接一个g的字符串

  [root@ www.linuxidc.com]# grep -n 'o\{2,\}g' regular_express.txt

  18:google is the best tools for search keyword.

  19:goooooogle yes!

  三、扩展资料:linux中grep命令之正则表达式

  匹配字符:这部分和基本正则表达式一样

  匹配次数:

  * :和基本正则表达式一样

  ? :基本正则表达式是\?,二这里没有\。

  {m,n} :相比基本正则表达式也是没有了\。

  + :匹配其前面的字符至少一次,相当于{1,}。

  位置锚定:和基本正则表达式一样。

  分组及引用:

  (string) :相比基本正则表达式也是没有了\。

   class="main">

linux中的grep命令的用法详解(2)

时间: 佳洲1085 分享

  或者:

  a|b :匹配a或b,注意a是指 | 的左边的整体,b也同理。比如 C|cat 表示的是 C或cat,而不是Cat或cat,如果要表示Cat或cat,则应该写为 (C|c)at 。记住(string)除了用于引用还用于分组。

  常用正则表达式部分就说到这里,以后用到在另行补充,如果你有兴趣也可以去网上查找其他文章来进一步了解。

3635303