Chinaunix首页 | 论坛 | 博客
  • 博客访问: 345382
  • 博文数量: 81
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 847
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-25 22:29
个人简介

执一不失,能君万物http://weidian.com/s/284520723?wfr=c

文章分类

全部博文(81)

文章存档

2016年(11)

2015年(70)

我的朋友

分类: LINUX

2015-07-01 23:12:02


点击(此处)折叠或打开

  1. 1.grep的退出状态
  2. [root@localhost ~]# grep root /etc/passwd
  3. root:x:0:0:root:/root:/bin/bash
  4. operator:x:11:0:operator:/root:/sbin/nologin
  5. 0: 表示成功;
  6. 1: 表示在所提供的文件无法找到匹配的pattern;
  7. 2: 表示参数中提供的文件不存在。
  8. 举例说明:
  9. [root@localhost ~]# echo $?
  10. 0
  11. [root@localhost ~]# grep root123 /etc/passwd
  12. [root@localhost ~]# echo $?
  13. 1
  14. [root@localhost ~]# grep root /etc/passwd123
  15. grep: /etc/passwd123: 没有那个文件或目录
  16. [root@localhost ~]# echo $?
  17. 2
  18. 2.grep中应用正则表达式的实例
  19. [root@localhost ~]# cat testfile
  20. northwest NW Charles Main 3.0 .98 3 34
  21. western WE Sharon Gray 5.3 .97 5 23
  22. southwest SW Lewis Dalsass 2.7 .8 2 18
  23. southern SO Suan Chin 5.1 .95 4 15
  24. southeast SE Patricia Hemenway 4.0 .7 4 17
  25. eastern EA TB Savage 4.4 .84 5 20
  26. northeast NE AM Main Jr. 5.1 .94 3 13
  27. north NO Margot Weber 4.5 .89 5 9
  28. central CT Ann Stephens 5.7 .94 5 13
  29. [root@localhost ~]# grep NW testfile #打印出testfile中所有包含NW的行。
  30. northwest NW Charles Main 3.0 .98 3 34
  31. [root@localhost ~]# grep '^n' testfile #打印出以n开头的行。
  32. northwest NW Charles Main 3.0 .98 3 34
  33. northeast NE AM Main Jr. 5.1 .94 3 13
  34. north NO Margot Weber 4.5 .89 5 9
  35. [root@localhost ~]# cat finduser
  36. #!/bin/bash
  37. #finduser
  38. who|grep $1[root@localhost ~]# grep 'user$' finduser #打印出以user结尾的行。
  39. #finduser
  40. [root@localhost ~]# grep '\.5' testfile #打印出所有包含.5的行。
  41. north NO Margot Weber 4.5 .89 5 9
  42. [root@localhost ~]# grep '^n' testfile #打印出所有以n开头的行。
  43. northwest NW Charles Main 3.0 .98 3 34
  44. northeast NE AM Main Jr. 5.1 .94 3 13
  45. north NO Margot Weber 4.5 .89 5 9
  46. [root@localhost ~]# grep [^0-9] testfile #打印出所有不是以0-9开头的行。
  47. northwest NW Charles Main 3.0 .98 3 34
  48. western WE Sharon Gray 5.3 .97 5 23
  49. southwest SW Lewis Dalsass 2.7 .8 2 18
  50. southern SO Suan Chin 5.1 .95 4 15
  51. southeast SE Patricia Hemenway 4.0 .7 4 17
  52. eastern EA TB Savage 4.4 .84 5 20
  53. northeast NE AM Main Jr. 5.1 .94 3 13
  54. north NO Margot Weber 4.5 .89 5 9
  55. central CT Ann Stephens 5.7 .94 5 13
  56. [root@localhost ~]# grep '[A-Z][A-Z] [A-Z]' testfile #打印出所有包含前两个字符是大写字符,后面紧跟一个空格及一个大写字母的行。
  57. eastern EA TB Savage 4.4 .84 5 20
  58. northeast NE AM Main Jr. 5.1 .94 3 13
  59. [root@localhost ~]# grep '[a-z]\{9\}' testfile #打印所有包含每个字符串至少有9个连续小写字符的字符串的行。
  60. northwest NW Charles Main 3.0 .98 3 34
  61. southwest SW Lewis Dalsass 2.7 .8 2 18
  62. southeast SE Patricia Hemenway 4.0 .7 4 17
  63. northeast NE AM Main Jr. 5.1 .94 3 13
  64. [root@localhost ~]# grep '\(3\)\.[0-9].*\1 *\1' testfile #第一个字符是3,紧跟着一个句点,然后是任意一个数字,然后是任意个任意字符,然后又是一个3,然后是制表符,然后又是一个3,需要说明的是,下面正则中的\1表示\(3\)
  65. northwest NW Charles Main 3.0 .98 3 34
  66. root@localhost ~]# grep '\<north' testfile #打印所有以north开头的单词的行
  67. northwest NW Charles Main 3.0 .98 3 34
  68. northeast NE AM Main Jr. 5.1 .94 3 13
  69. north NO Margot Weber 4.5 .89 5 9
  70. [root@localhost ~]# grep '\<north\>' testfile #打印所有包含单词north的行。
  71. north NO Margot Weber 4.5 .89 5 9
  72. root@localhost ~]# grep '^n\w*' testfile #打印所有包含单词north的行。
  73. northwest NW Charles Main 3.0 .98 3 34
  74. northeast NE AM Main Jr. 5.1 .94 3 13
  75. north NO Margot Weber 4.5 .89 5 9
  76. 3.扩展grep(grep -E 或者 egrep)
  77. [root@localhost ~]# egrep 'NW|EA' testfile #打印所有包含NW或EA的行。如果不是使用egrep,而是grep,将不会有结果查出。
  78. northwest NW Charles Main 3.0 .98 3 34
  79. eastern EA TB Savage 4.4 .84 5 20
  80. [root@localhost ~]# grep 'NW|EA' testfile
  81. [root@localhost ~]#
  82. [root@localhost ~]# grep 'NW\|EA' testfile #对于标准grep,如果在扩展元字符前面加\,grep会自动启用扩展选项-E。
  83. northwest NW Charles Main 3.0 .98 3 34
  84. eastern EA TB Savage 4.4 .84 5 20
  85. [root@localhost ~]# egrep '3+' testfile #所有包含一个或多个3的行
  86. northwest NW Charles Main 3.0 .98 3 34
  87. western WE Sharon Gray 5.3 .97 5 23
  88. northeast NE AM Main Jr. 5.1 .94 3 13
  89. central CT Ann Stephens 5.7 .94 5 13
  90. [root@localhost ~]# egrep '2\.?[0-9]' testfile #首先含有2字符,其后紧跟着0个或1个点,后面再是0和9之间的数字。
  91. western WE Sharon Gray 5.3 .97 5 23
  92. southwest SW Lewis Dalsass 2.7 .8 2 18
  93. eastern EA TB Savage 4.4 .84 5 20
  94. [root@localhost ~]# egrep '(no)+' testfile #打印一个或者多个连续的no的行。
  95. northwest NW Charles Main 3.0 .98 3 34
  96. northeast NE AM Main Jr. 5.1 .94 3 13
  97. north NO Margot Weber 4.5 .89 5 9
  98. [root@localhost ~]# grep -E '\w+\W+[ABC]' testfile
  99. northwest NW Charles Main 3.0 .98 3 34
  100. southern SO Suan Chin 5.1 .95 4 15
  101. northeast NE AM Main Jr. 5.1 .94 3 13
  102. central CT Ann Stephens 5.7 .94 5 13
  103. [root@localhost ~]# egrep '[Ss](h|u)' testfile #以S或s开头,紧跟着h或者u的行。
  104. western WE Sharon Gray 5.3 .97 5 23
  105. southern SO Suan Chin 5.1 .95 4 15
  106. [root@localhost ~]# egrep 'w(es)t.*\1' testfile #west开头,其中es为\1的值,后面紧跟着任意数量的任意字符,最后还有一个es出现在该行。
  107. northwest NW Charles Main 3.0 .98 3 34
  108. 4.grep选项
  109.     这里先列出grep常用的命令行选项:
  110.     -c 只显示有多少行匹配,而不具体显示匹配的行。
  111.     -h 不显示文件名。
  112.     -i 在字符串比较的时候忽略大小写。
  113.     -l 只显示包含匹配模板的行的文件名清单。
  114.     -L 只显示不包含匹配模板的行的文件名清单。
  115.     -n 在每一行前面打印改行在文件中的行数。
  116.     -v 反向检索,只显示不匹配的行。
  117.     -w 只显示完整单词的匹配。
  118.     -x 只显示完整行的匹配。
  119.     -r/-R 如果文件参数是目录,该选项将递归搜索该目录下的所有子目录和文件。
  120. [root@localhost ~]# grep -n '^south' testfile
  121. 3:southwest SW Lewis Dalsass 2.7 .8 2 18
  122. 4:southern SO Suan Chin 5.1 .95 4 15
  123. 5:southeast SE Patricia Hemenway 4.0 .7 4 17
  124. [root@localhost ~]# grep -i 'pat' testfile
  125. southeast SE Patricia Hemenway 4.0 .7 4 17
  126. [root@localhost ~]# grep -i 'pat' testfile
  127. southeast SE Patricia Hemenway 4.0 .7 4 17
  128. [root@localhost ~]# grep -v 'SHAN DONG' testfile
  129. northwest NW Charles Main 3.0 .98 3 34
  130. western WE Sharon Gray 5.3 .97 5 23
  131. southwest SW Lewis Dalsass 2.7 .8 2 18
  132. southern SO Suan Chin 5.1 .95 4 15
  133. southeast SE Patricia Hemenway 4.0 .7 4 17
  134. eastern EA TB Savage 4.4 .84 5 20
  135. northeast NE AM Main Jr. 5.1 .94 3 13
  136. north NO Margot Weber 4.5 .89 5 9
  137. central CT Ann Stephens 5.7 .94 5 13
  138. [root@localhost ~]# grep -l 'ss' testfile
  139. testfile
  140. [root@localhost ~]# grep -c 'west' testfile
  141. 3
  142. [root@localhost ~]# grep -w 'north' testfile
  143. north NO Margot Weber 4.5 .89 5 9
  144. [root@localhost ~]# grep -C 2 Patricia testfile #打印匹配行及其上下各两行。
  145. southwest SW Lewis Dalsass 2.7 .8 2 18
  146. southern SO Suan Chin 5.1 .95 4 15
  147. southeast SE Patricia Hemenway 4.0 .7 4 17
  148. eastern EA TB Savage 4.4 .84 5 20
  149. northeast NE AM Main Jr. 5.1 .94 3 13
  150. [root@localhost ~]# grep -B 2 Patricia testfile #打印匹配行及其前两行。
  151. southwest SW Lewis Dalsass 2.7 .8 2 18
  152. southern SO Suan Chin 5.1 .95 4 15
  153. southeast SE Patricia Hemenway 4.0 .7 4 17
  154. [root@localhost ~]# grep -A 2 Patricia testfile #打印匹配行及其后两行。
  155. southeast SE Patricia Hemenway 4.0 .7 4 17
  156. eastern EA TB Savage 4.4 .84 5 20
  157. northeast NE AM Main Jr. 5.1 .94 3 13


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

上一篇:正则表达式1

下一篇:sed的使用

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