Chinaunix首页 | 论坛 | 博客
  • 博客访问: 891760
  • 博文数量: 113
  • 博客积分: 3160
  • 博客等级: 少校
  • 技术积分: 1801
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-19 10:09
文章分类

全部博文(113)

分类: Python/Ruby

2012-09-18 17:42:56

* grep命令基础

grep命令在文件中全局查找指定的正则表达式,并且打印所有包含这个表达式的行。

=命令格式:=
: grep [option] 'pattern' filenames

其中引号包括起来的pattern指的是正则表达式匹配的模式,filenames则是一个
或者多个文件名。

** option

下面列出了grep常用的几个选项。


  1. |----+------------------------|
  2. | -c | 显示匹配到的行数 |
  3. |----+------------------------|
  4. | -i | 比较时忽略大小字 |
  5. |----+------------------------|
  6. | -l | 只列出所在文件的文件名 |
  7. |----+------------------------|
  8. | -v | 显示不匹配的行 |
  9. |----+------------------------|


** pattern

关于匹配的模式,其实就是一系列的元字符加上普通字符组成的一个字符串,使
用该字符串可以表达更多更准确的含义。下面是grep命令支持的正则表达式元字
符。


  1. |----------+----------------------------------|
  2. | ^ | 行首定位符 |
  3. |----------+----------------------------------|
  4. | $ | 行为定位符 |
  5. |----------+----------------------------------|
  6. | \< | 词首定位符 |
  7. |----------+----------------------------------|
  8. | \> | 词尾定位符 |
  9. |----------+----------------------------------|
  10. | . | 匹配一个字符 |
  11. |----------+----------------------------------|
  12. | * | 匹配零个以上字符 |
  13. |----------+----------------------------------|
  14. | [] | 匹配一组字符中的任何一个 |
  15. |----------+----------------------------------|
  16. | [^] | 不匹配一组字符中的任何一个 |
  17. |----------+----------------------------------|
  18. | \(\) | 标记匹配的字符 |
  19. |----------+----------------------------------|
  20. | x\{m\} | 字符出现m次 |
  21. |----------+----------------------------------|
  22. | x\{m,\} | 字符至少出现m次 |
  23. |----------+----------------------------------|
  24. | x\{m,n\} | 字符至少出现m次,但是不能超所n次 |
  25. |----------+----------------------------------|


** filenames

grep命令可以对一个到多个文件进行查找。

* 实例解析


首先我们先生生成一个文件,以便于后面的测试。


  1. : man man | head -48 | tail -10 > test
  2. : cat test



生成结果如下:


  1. ~$ cat test
  2. 2 System calls (functions provided by the kernel)
  3. 3 Library calls (functions within program libraries)
  4. 4 Special files (usually found in /dev)
  5. 5 File formats and conventions eg /etc/passwd
  6. 6 Games
  7. 7 Miscellaneous (including macro packages and conventions), e.g.
  8. man(7), groff(7)
  9. 8 System administration commands (usually only for root)
  10. 9 Kernel routines [Non standard]

** 打印以XX结尾的行



  1. ~$ grep 'passwd' test
  2. 5 File formats and conventions eg /etc/passwd
  3. # 打印以passwd结尾的行


** 打印以XX开头的行


  1. ~$ grep '^ ' test
  2. 2 System calls (functions provided by the kernel)
  3. 3 Library calls (functions within program libraries)
  4. 4 Special files (usually found in /dev)
  5. 5 File formats and conventions eg /etc/passwd
  6. 6 Games
  7. 7 Miscellaneous (including macro packages and conventions), e.g.
  8. man(7), groff(7)
  9. 8 System administration commands (usually only for root)
  10. 9 Kernel routines [Non standard]
  11. # 打印以空格开始的行


** 打印包含单词XX的行


  1. ~$ grep '\' test
  2. 2 System calls (functions provided by the kernel)
  3. 3 Library calls (functions within program libraries)
  4. # 打印包含单词calls的行


** 打印出现特定个数字符的行
 

  1. ~$ grep 'l\{2,5\}' test
  2. 2 System calls (functions provided by the kernel)
  3. 3 Library calls (functions within program libraries)
  4. 4 Special files (usually found in /dev)
  5. 7 Miscellaneous (including macro packages and conventions), e.g.
  6. 8 System administration commands (usually only for root)
  7. # 打印出现l在2到5个的行。



grep命令主要用于对特定文本进行分析,查找到相关的行。在文本过滤上,sed和
awk比grep更精准,后面会一一介绍它们的使用方法。

* grep命令回顾

** 打印包含Tom这个词的行

  1. : grep '\' file
  2. : grep '\bTom\b' file
  3. : grep -w 'Tom' file # 某些grep版本不支持


** 打印包含Tom Sam的行

: grep 'Tom Sam' file

** 打印以Tom开头的行

: grep '^Tom' file

** 打印以Tom结尾的行

: grep 'Tom$' file

** 打印至少包含一个大写字母/小写字母/数字的行

: grep '[A-Z]' file

: grep '[a-z]' file

: grep '[0-9]' file


** 取消大小写敏感

: grep -i 'sam' file  # 这会打印所有包含Sam SAM sAM等所有变形的行

** 打印匹配的行号

: grep -n 'Tom Sam' file

** 打印以一个或者多个空格开头的行(Egrep)

: egrep '^ *' file  # 零个以上的空格

: grep -E '^ +' file  # 一个以上的空格


** 打印like或者hate的行

: grep 'like|hate' file

阅读(3489) | 评论(0) | 转发(0) |
0

上一篇:AWK入门

下一篇:set命令解析

给主人留下些什么吧!~~