grep命令解析

grep命令解析


Table of Contents

  • 1 grep命令基础
    • 1.1 option
    • 1.2 pattern
    • 1.3 filenames
  • 2 实例解析
    • 2.1 打印以XX结尾的行
    • 2.2 打印以XX开头的行
    • 2.3 打印包含单词XX的行
    • 2.4 打印出现特定个数字符的行
  • 3 grep命令回顾
    • 3.1 打印包含Tom这个词的行
    • 3.2 打印包含Tom Sam的行
    • 3.3 打印以Tom开头的行
    • 3.4 打印以Tom结尾的行
    • 3.5 打印至少包含一个大写字母/小写字母/数字的行
    • 3.6 取消大小写敏感
    • 3.7 打印匹配的行号
    • 3.8 打印以一个或者多个空格开头的行(Egrep)
    • 3.9 打印like或者hate的行


1 grep命令基础


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


命令格式:

grep [option] 'pattern' filenames


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


1.1 option


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


-c 显示匹配到的行数
-i 比较时忽略大小字
-l 只列出所在文件的文件名
-v 显示不匹配的行


1.2 pattern


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


$ 行为定位符
\< 词首定位符
\> 词尾定位符
. 匹配一个字符
* 匹配零个以上字符
[] 匹配一组字符中的任何一个
[] 不匹配一组字符中的任何一个
\(\) 标记匹配的字符
x\{m\} 字符出现m次
x\{m,\} 字符至少出现m次
x\{m,n\} 字符至少出现m次,但是不能超所n次


1.3 filenames


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


2 实例解析


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

man man | head -48 | tail -10 > test
cat test 


生成结果如下:

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


2.1 打印以XX结尾的行

~$ grep 'passwd' test

       5   File formats and conventions eg /etc/passwd

# 打印以passwd结尾的行


2.2 打印以XX开头的行

~$ grep '^ ' test

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

# 打印以空格开始的行


2.3 打印包含单词XX的行

~$ grep '\<calls\>' test

       2   System calls (functions provided by the kernel)
       3   Library calls (functions within program libraries)

# 打印包含单词calls的行


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

~$ grep 'l\{2,5\}' test

       2   System calls (functions provided by the kernel)
       3   Library calls (functions within program libraries)
       4   Special files (usually found in /dev)
       7   Miscellaneous  (including  macro  packages  and  conventions), e.g.
       8   System administration commands (usually only for root)

# 打印出现l在2到5个的行。


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


3 grep命令回顾


3.1 打印包含Tom这个词的行

grep '\<Tom\>' file
grep '\bTom\b' file
grep -w 'Tom' file  # 某些grep版本不支持


3.2 打印包含Tom Sam的行

grep 'Tom Sam' file


3.3 打印以Tom开头的行

grep '^Tom' file


3.4 打印以Tom结尾的行

grep 'Tom$' file


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

grep '[A-Z]' file
grep '[a-z]' file
grep '[0-9]' file


3.6 取消大小写敏感

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


3.7 打印匹配的行号

grep -n 'Tom Sam' file


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

egrep '^ *' file  # 零个以上的空格
grep -E '^ +' file  # 一个以上的空格


3.9 打印like或者hate的行

grep 'like|hate' file




Date: 2012-09-09 日

Author: hic

Org version 7.9.1 with Emacs version 23

Validate XHTML 1.0

你可能感兴趣的:(grep命令解析)