找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 5054|回复: 0

Linux Shell常用技巧(二)

[复制链接]
发表于 2012-4-21 10:14:43 | 显示全部楼层 |阅读模式
  1. 七. grep家族:
  2.    
  3.    1.  grep退出状态:
  4.     0: 表示成功;
  5.     1: 表示在所提供的文件无法找到匹配的pattern;
  6.     2: 表示参数中提供的文件不存在。
  7.     见如下示例:
  8.     /> grep 'root' /etc/passwd
  9.     root:x:0:0:root:/root:/bin/bash
  10.     operator:x:11:0:operator:/root:/sbin/nologin
  11.     /> echo $?
  12.     0
  13.    
  14.     /> grep 'root1' /etc/passwd  #用户root1并不存在
  15.     /> echo $?
  16.     1
  17.    
  18.     /> grep 'root' /etc/passwd1  #这里的/etc/passwd1文件并不存在
  19.     grep: /etc/passwd1: No such file or directory
  20.     /> echo $?
  21.     2
  22.    
  23.    2.  grep中应用正则表达式的实例:
  24.     需要说明的是下面所涉及的正则表达式在上一篇中已经给出了详细的说明,因此在看下面例子的时候,可以与前一篇的正则说明部分结合着看。
  25.     /> cat testfile
  26.     northwest        NW      Charles Main           3.0     .98     3       34
  27.     western           WE       Sharon Gray          5.3     .97     5       23
  28.     southwest       SW       Lewis Dalsass         2.7     .8       2       18
  29.     southern         SO       Suan Chin               5.1     .95     4       15
  30.     southeast       SE        Patricia Hemenway    4.0     .7       4       17
  31.     eastern           EA        TB Savage              4.4     .84     5       20
  32.     northeast        NE        AM Main Jr.              5.1     .94     3       13
  33.     north              NO       Margot Weber          4.5     .89     5       9
  34.     central            CT        Ann Stephens          5.7     .94     5       13
  35.    
  36.     /> grep NW testfile     #打印出testfile中所有包含NW的行。
  37.     northwest       NW      Charles Main        3.0     .98     3       34
  38.    
  39.     /> grep '^n' testfile   #打印出以n开头的行。
  40.     northwest       NW      Charles Main        3.0     .98     3       34
  41.     northeast        NE       AM Main Jr.          5.1     .94     3       13
  42.     north              NO      Margot Weber      4.5     .89     5       9
  43.    
  44.     /> grep '4来自:[url]http://www.cnblogs.com/stephen-liu74/archive/2011/11/14/2243694.html[/url]
  45. testfile   #打印出以4结尾的行。
  46.     northwest       NW      Charles Main        3.0     .98     3       34
  47.    
  48.     /> grep '5\..' testfile #打印出第一个字符是5,后面跟着一个.字符,在后面是任意字符的行。
  49.     western         WE      Sharon Gray         5.3     .97     5       23
  50.     southern        SO      Suan Chin             5.1     .95     4       15
  51.     northeast       NE      AM Main Jr.            5.1     .94     3       13
  52.     central           CT      Ann Stephens        5.7     .94     5       13
  53.    
  54.     /> grep '\.5' testfile  #打印出所有包含.5的行。
  55.     north           NO      Margot Weber        4.5     .89     5       9
  56.     /> grep '^[we]' testfile #打印出所有以w或e开头的行。
  57.     western         WE      Sharon Gray         5.3     .97     5       23
  58.     eastern          EA      TB Savage            4.4     .84     5       20
  59.    
  60.     /> grep '[^0-9]' testfile #打印出所有不是以0-9开头的行。
  61.     northwest       NW     Charles Main             3.0     .98      3       34
  62.     western          WE      Sharon Gray             5.3     .97     5       23
  63.     southwest       SW     Lewis Dalsass           2.7     .8       2       18
  64.     southern         SO      Suan Chin                5.1     .95     4       15
  65.     southeast        SE      Patricia Hemenway     4.0     .7      4       17
  66.     eastern           EA      TB Savage                4.4     .84     5       20
  67.     northeast        NE      AM Main Jr.                5.1     .94     3       13
  68.     north              NO      Margot Weber           4.5     .89     5       9
  69.     central            CT      Ann Stephens            5.7     .94     5       13
  70.     /> grep '[A-Z][A-Z] [A-Z]' testfile #打印出所有包含前两个字符是大写字符,后面紧跟一个空格及一个大写字母的行。
  71.     eastern          EA      TB Savage       4.4     .84     5       20
  72.     northeast       NE      AM Main Jr.      5.1     .94     3       13
  73.     注:在执行以上命令时,如果不能得到预期的结果,即grep忽略了大小写,导致这一问题的原因很可能是当前环境的本地化的设置问题。对于以上命令,如果我将当前语言设置为en_US的时候,它会打印出所有的行,当我将其修改为中文环境时,就能得到我现在的输出了。
  74.     /> export LANG=zh_CN  #设置当前的语言环境为中文。
  75.     /> export LANG=en_US  #设置当前的语言环境为美国。
  76.     /> export LANG=en_Br  #设置当前的语言环境为英国。
  77.    
  78.     /> grep '[a-z]\{9\}' testfile #打印所有包含每个字符串至少有9个连续小写字符的字符串的行。
  79.     northwest        NW      Charles Main          3.0     .98     3       34
  80.     southwest       SW      Lewis Dalsass         2.7     .8       2       18
  81.     southeast        SE      Patricia Hemenway   4.0     .7       4       17
  82.     northeast        NE      AM Main Jr.              5.1     .94     3       13
  83.    
  84.     #第一个字符是3,紧跟着一个句点,然后是任意一个数字,然后是任意个任意字符,然后又是一个3,然后是制表符,然后又是一个3,需要说明的是,下面正则中的\1表示\(3\)。
  85.     /> grep '\(3\)\.[0-9].*\1    *\1' testfile
  86.     northwest       NW      Charles Main        3.0     .98     3       34
  87.    
  88.     /> grep '\<north' testfile    #打印所有以north开头的单词的行。
  89.     northwest       NW      Charles Main          3.0     .98     3       34
  90.     northeast        NE       AM Main Jr.            5.1     .94     3       13
  91.     north              NO      Margot Weber        4.5     .89     5       9
  92.    
  93.     /> grep '\<north\>' testfile  #打印所有包含单词north的行。
  94.     north           NO      Margot Weber        4.5     .89     5       9
  95.    
  96.     /> grep '^n\w*' testfile      #第一个字符是n,后面是任意字母或者数字。
  97.     northwest       NW     Charles Main          3.0     .98     3       34
  98.     northeast        NE      AM Main Jr.            5.1     .94     3       13
  99.     north             NO      Margot Weber        4.5     .89     5       9
  100.    
  101.     3.  扩展grep(grep -E 或者 egrep):
  102.     使用扩展grep的主要好处是增加了额外的正则表达式元字符集。下面我们还是继续使用实例来演示扩展grep。
  103.     /> egrep 'NW|EA' testfile     #打印所有包含NW或EA的行。如果不是使用egrep,而是grep,将不会有结果查出。
  104.     northwest       NW      Charles Main        3.0     .98     3       34
  105.     eastern         EA      TB Savage           4.4     .84     5       20
  106.    
  107.     /> grep 'NW\|EA' testfile     #对于标准grep,如果在扩展元字符前面加\,grep会自动启用扩展选项-E。
  108.     northwest       NW      Charles Main        3.0     .98     3       34
  109.     eastern           EA       TB Savage           4.4     .84     5       20
  110.    
  111.     /> egrep '3+' testfile
  112.     /> grep -E '3+' testfile
  113.     /> grep '3\+' testfile        #这3条命令将会打印出相同的结果,即所有包含一个或多个3的行。
  114.     northwest       NW      Charles Main         3.0     .98     3       34
  115.     western          WE      Sharon Gray         5.3     .97     5       23
  116.     northeast        NE       AM Main Jr.           5.1     .94     3       13
  117.     central            CT       Ann Stephens       5.7     .94     5       13
  118.    
  119.     /> egrep '2\.?[0-9]' testfile
  120.     /> grep -E '2\.?[0-9]' testfile
  121.     /> grep '2\.\?[0-9]' testfile #首先含有2字符,其后紧跟着0个或1个点,后面再是0和9之间的数字。
  122.     western         WE       Sharon Gray          5.3     .97     5       23
  123.     southwest      SW      Lewis Dalsass         2.7     .8      2       18
  124.     eastern          EA       TB Savage             4.4     .84     5       20
  125.    
  126.     /> egrep '(no)+' testfile
  127.     /> grep -E '(no)+' testfile
  128.     /> grep '\(no\)\+' testfile   #3个命令返回相同结果,即打印一个或者多个连续的no的行。
  129.     northwest       NW      Charles Main        3.0     .98     3       34
  130.     northeast        NE       AM Main Jr.          5.1     .94     3       13
  131.     north              NO      Margot Weber      4.5     .89     5       9
  132.    
  133.     /> grep -E '\w+\W+[ABC]' testfile #首先是一个或者多个字母,紧跟着一个或者多个非字母数字,最后一个是ABC中的一个。
  134.     northwest       NW     Charles Main       3.0     .98     3       34
  135.     southern        SO      Suan Chin           5.1     .95     4       15
  136.     northeast       NE      AM Main Jr.          5.1     .94     3       13
  137.     central           CT      Ann Stephens      5.7     .94     5       13
  138.    
  139.     /> egrep '[Ss](h|u)' testfile
  140.     /> grep -E '[Ss](h|u)' testfile
  141.     /> grep '[Ss]\(h\|u\)' testfile   #3个命令返回相同结果,即以S或s开头,紧跟着h或者u的行。
  142.     western         WE      Sharon Gray       5.3     .97     5       23
  143.     southern        SO      Suan Chin          5.1     .95     4       15
  144.    
  145.     /> egrep 'w(es)t.*\1' testfile    #west开头,其中es为\1的值,后面紧跟着任意数量的任意字符,最后还有一个es出现在该行。
  146.     northwest       NW      Charles Main        3.0     .98     3       34
  147.    
  148.     4.  grep选项:
  149.     这里先列出grep常用的命令行选项:
  150. 选项        说明
  151. -c        只显示有多少行匹配,而不具体显示匹配的行。
  152. -h        不显示文件名。
  153. -i        在字符串比较的时候忽略大小写。
  154. -l        只显示包含匹配模板的行的文件名清单。
  155. -L        只显示不包含匹配模板的行的文件名清单。
  156. -n        在每一行前面打印改行在文件中的行数。
  157. -v        反向检索,只显示不匹配的行。
  158. -w        只显示完整单词的匹配。
  159. -x        只显示完整行的匹配。
  160. -r/-R        如果文件参数是目录,该选项将递归搜索该目录下的所有子目录和文件。
  161.     /> grep -n '^south' testfile  #-n选项在每一个匹配行的前面打印行号。
  162.     3:southwest     SW      Lewis Dalsass         2.7     .8      2       18
  163.     4:southern       SO      Suan Chin               5.1     .95     4       15
  164.     5:southeast      SE      Patricia Hemenway    4.0     .7      4       17
  165.     /> grep -i 'pat' testfile     #-i选项关闭了大小写敏感。
  166.     southeast       SE      Patricia Hemenway       4.0     .7      4       17
  167.     /> grep -v 'Suan Chin' testfile #打印所有不包含Suan Chin的行。
  168.     northwest       NW      Charles Main          3.0     .98     3       34
  169.     western          WE      Sharon Gray           5.3     .97    5       23
  170.     southwest       SW      Lewis Dalsass        2.7     .8      2       18
  171.     southeast        SE      Patricia Hemenway   4.0     .7      4       17
  172.     eastern           EA      TB Savage              4.4     .84     5       20
  173.     northeast        NE      AM Main Jr.             5.1     .94     3       13
  174.     north              NO      Margot Weber        4.5     .89     5       9
  175.     central            CT      Ann Stephens         5.7     .94     5       13
  176.     /> grep -l 'ss' testfile  #-l使得grep只打印匹配的文件名,而不打印匹配的行。
  177.     testfile
  178.     /> grep -c 'west' testfile #-c使得grep只打印有多少匹配模板的行。
  179.     3
  180.     /> grep -w 'north' testfile #-w只打印整个单词匹配的行。
  181.     north           NO      Margot Weber    4.5     .89     5       9
  182.     /> grep -C 2 Patricia testfile #打印匹配行及其上下各两行。
  183.     southwest      SW     Lewis Dalsass         2.7     .8       2       18
  184.     southern        SO      Suan Chin              5.1     .95     4       15
  185.     southeast       SE      Patricia Hemenway   4.0     .7      4       17
  186.     eastern          EA      TB Savage              4.4     .84     5       20
  187.     northeast       NE      AM Main Jr.             5.1     .94     3       13
  188.     /> grep -B 2 Patricia testfile #打印匹配行及其前两行。
  189.     southwest      SW      Lewis Dalsass         2.7     .8      2       18
  190.     southern        SO      Suan Chin               5.1     .95    4       15
  191.     southeast       SE      Patricia Hemenway   4.0     .7      4       17
  192.     /> grep -A 2 Patricia testfile #打印匹配行及其后两行。
  193.     southeast       SE      Patricia Hemenway   4.0     .7      4       17
  194.     eastern           EA      TB Savage              4.4     .84     5       20
  195.     northeast       NE       AM Main Jr.             5.1     .94     3       13
复制代码
来自:http://www.cnblogs.com/stephen-l ... /11/14/2243694.html

目录:
Linux Shell常用技巧(一)
http://www.acejoy.com/thread-4312-1-1.html
一. 特殊文件: /dev/null和/dev/tty
二. 简单的命令跟踪
三. 正则表达式基本语法描述
四. 使用cut命令选定字段
五. 计算行数、字数以及字符数
六. 提取开头或结尾数行

Linux Shell常用技巧(二)
http://www.acejoy.com/thread-4313-1-1.html
七. grep家族

Linux Shell常用技巧(三)
http://www.acejoy.com/thread-4314-1-1.html
八. 流编辑器sed

Linux Shell常用技巧(四)
http://www.acejoy.com/thread-4315-1-1.html
九. awk实用功能
十. awk表达式功能

Linux Shell常用技巧(五)
http://www.acejoy.com/thread-4316-1-1.html
十一.awk编程

Linux Shell常用技巧(六)
http://www.acejoy.com/thread-4317-1-1.html
十二.行的排序命令sort
十三.删除重复行的命令uniq
十四.文件压缩解压命令tar
十五.大文件拆分命令split

Linux Shell常用技巧(七)
http://www.acejoy.com/thread-4318-1-1.html
十六.文件查找命令find
十七.xargs命令

Linux Shell常用技巧(八)
http://www.acejoy.com/thread-4319-1-1.html
十八.和系统运行状况相关的Shell命令

Linux Shell常用技巧(九)
http://www.acejoy.com/thread-4320-1-1.html
十九.和系统运行进程相关的Shell命令

Linux Shell常用技巧(十)
http://www.acejoy.com/thread-4321-1-1.html
二十.通过管道组合Shell命令获取系统运行数据
二十一.通过管道组合Shell命令进行系统管理

Linux Shell常用技巧(十一)
http://www.acejoy.com/thread-4322-1-1.html
二十二.交互式使用Bash Shell

Linux Shell常用技巧(十二)
http://www.acejoy.com/thread-4323-1-1.html
二十三.Bash Shell编程

您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-8 04:13 , Processed in 0.014360 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表