grep命令解析

grep命令解析


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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值