linux命令九:grep

来源:互联网 发布:cf天赐软件视频 编辑:程序博客网 时间:2024/04/25 12:00


grep命令

1、grep命令基本用法
 
grep命令是支持正则表达式的一个多用途文本搜索工具,grep的一般格式为:
 
grep [选项] [模式] [文件...]
 
grep命令由选项、模式和文件三部分组成,它在一个或多个文件中搜索满足模式的文本行,模式后的所有字符串被看做文件名,文件名可以有多个,搜索的结果被打印到屏幕,不影响原文件的内容。Grep命令的选项用于对搜索过程进行补充说明。


grep命令选项及其意义:
 
-c              #只输出匹配行的数量
 
-i              #搜索时忽略大小写
 
-h              #查询多文件时不显示文件名
 
-l              #只列出符合匹配的文件名,而不列出具体的匹配行
 
-n              #列出所有的匹配行,并显示行号
 
-s              #不显示不存在或无匹配文本的错误信息
 
-v              #显示不包含匹配文本的所有行
 
-w              #匹配整词
 
-x              #匹配整行
 
-r              #递归搜索,不仅搜索当前工作目录,而且搜索子目录
 
-q              #禁止输出任何结果,以退出状态表示搜索是否成功
 
-b              #打印匹配行距文件头部的偏移量,以字节为单位
 
-o              #与-b选项结合使用,打印匹配的词距文件头部的偏移量,以字节为单位
 
-E              #支持扩展的正则表达式
 
-F              #不支持正则表达式,按照字符串的字面意思进行匹配
 
 
 
grep命令的模式十分灵活,可以是字符串,也可以是变量,还可以是正则表达式。需要说明的是,无论模式是何种形式,只要模式中包含空格,就需要使用双引号将模式引起来,如果不加双引号,空格后的单词容易被误认为是文件名。大部分情况下,使用单引号将模式引起来也是可以的。
 
例:模式包含空格时,是否使用双引号的区别
 
WORDLIST文件的内容:

hello, caicai. world: watch, world caicaihello message
 
message world watch hello into the he shelast into.
 
last save hello caicai, world: message.
 
#搜索WORDLIST文件中包含watch字符串的行,不需要引号引起模式
 
执行:grep watch WORDLIST

结果:hello, caicai. world:watch, world caicai hello message
 
      message world watch hello into the he she lastinto.
 
#搜索WORDLIST文件中包含hellocaicai字符串的行,不用引号将hello caicai引起来的结果
 
执行:grep hello caicaiWORDLIST

结果:grep: caicai: 没有那个文件或目录
 
      #Shell将caicai解析为文件名,提示没有此文件的错误
 
      #下面给出WORDLIST文件中包含hello字符串的行
 
      WORDLIST:hello, caicai. world: watch, worldcaicai hello message
 
      WORDLIST:message world watch hello into thehe she last into.
 
      WORDLIST:last save hello caicai, world:message.
 
#搜索WORDLIST文件中包含hellocaicai字符串的行,用引号将hello caicai引起来的结果
 
执行:grep "hellocaicai" WORDLIST

结果:last save hellocaicai, world: message.
 
 
 
例:grep的多文件查询
 
FILE1文件的内容:
 
Shanghai Jiaotong University
 
University of Toronto
 
Beijing University
 
Southeast University
 
Victory University
 
FILE2文件的内容:
 
Shanghai
 
Toronto
 
Beijing
 
Nanjing
 
Melbourne
 
执行:grep Beijing FILE1FILE2
 
结果:FILE1:BeijingUniversity
 
      FILE2:Beijing
 
 
 
例:用通配表示多文件
 
执行:grep Beijing FILE?
 
结果:FILE1:BeijingUniversity
 
      FILE2:Beijing
 
 
 
1、-c选项
 
-c选项表示输出匹配字符串行的数量,默认情况下,grep命令打印出包含模式的所有行,一旦加上-c选项,就只显示包含模式行的数量。
 
例:grep -c的用法
 
执行:grep -c Beijing FILE?
 
结果:FILE1:1                                              #FILE1文件中有1行包含Beijing

      FILE2:1
 
 
 
2、-n选项
 
-n选项列出所有的匹配行,并显示行号。默认情况下,grep搜索单个文件时,只显示每行的内容,搜索多个文件时,显示文件名及每行的内容,加上-n选项后,将在行内容前附加显示行号。
 
例:grep -n的用法
 
执行:grep -n Beijing FILE?
 
结果:FILE1:3:BeijingUniversity            #FILE1文件的第3行
 
      FILE2:3:Beijing
 
 
 
3、-v选项
 
-v选项显示不包含模式的所有行。
 
例:grep -v的用法
 
执行:grep -vc BeijingFILE?          #同时使用-v和-c选项
 
结果:FILE1:4                        #FILE1文件中有4行不包含Beijing字符串
 
      FILE2:4
 
 
 
4、-i选项
 
默认情况下,grep命令对大小写是敏感的,如果加上-i选项就表示grep命令不区分大小写。
 
 
 
5、-h选项
 
-h选项表示查询多文件时不显示文件名,默认情况下,grep命令查询多个文件时,在匹配行之前显示文件名,加上-h选项后,grep命令将不再显示文件名。
 
例:grep -h的用法
 
执行:grep -h Beijing FILE?
 
结果:Beijing University                            #在匹配行前不再显示文件名了
 
      Beijing
 
 
 
6、-l选项
 
-l选项表示只列出符合匹配的文件名,而不列出具体匹配行。
 
 
 
例:grep -l的用法
 
执行:grep -l Beijing FILE?
 
结果:FILE1                                        #只显示包含Beijing字符串的文件名
 
      FILE2
 
 
 
7、-s选项
 
-s选项表示不显示不存在或无匹配文本的错误信息,默认情况下,grep在待搜索文件不存在或搜索不到符合模式的文本行时将打印错误信息。
 
例:grep -s的用法
 
执行:grep hello caicaiWORDLIST                  #未使用-s选项
 
结果:grep: caicai: 没有那个文件或目录            #打印了错误信息
 
      WORDLIST:hello, caicai. world: watch, worldcaicai hello message
 
      WORDLIST:message world watch hello into thehe she last into.
 
      WORDLIST:last save hello caicai, world:message.
 
执行:grep -s hello caicaiWORDLIST            #使用-s选项后,不打印错误信息
 
结果:WORDLIST:hello,caicai. world: watch, world caicai hello message
 
      WORDLIST:message world watch hello into thehe she last into.
 
      WORDLIST:last save hello caicai, world:message.


pattern正则表达式主要参数:
\: 忽略正则表达式中特殊字符的原有含义。
^:匹配正则表达式的开始行。
$: 匹配正则表达式的结束行。
\<:从匹配正则表达 式的行开始。
\>:到匹配正则表达式的行结束。
[ ]:单个字符,如[A]即A符合要求 。
[ - ]:范围,如[A-Z],即A、B、C一直到Z都符合要求 。
。:所有的单个字符。
* :有字符,长度可以为0。


grep命令使用简单实例
$ grep ‘test’ d*
显示所有以d开头的文件中包含 test的行。
$ grep ‘test’ aa bb cc
显示在aa,bb,cc文件中匹配test的行。
$ grep ‘[a-z]\{5\}’ aa
显示所有包含每个字符串至少有5个连续小写字符的字符串的行。
$ grep ‘w\(es\)t.*\1′ aa
如果west被匹配,则es就被存储到内存中,并标记为1,然后搜索任意个字符(.*),这些字符后面紧跟着 另外一个es(\1),找到就显示该行。如果用egrep或grep -E,就不用”\”号进行转义,直接写成’w(es)t.*\1′就可以了。

grep命令使用复杂实例
假设正在’/usr/src/Linux/Doc’目录下搜索带字符 串’magic’的文件:
$ grep magic /usr/src/Linux/Doc/*
sysrq.txt:* How do I enable the magic SysRQ key?
sysrq.txt:* How do I use the magic SysRQ key?
其中文件’sysrp.txt’包含该字符串,讨论的是 SysRQ 的功能。
默认情况下,’grep’只搜索当前目录。如果 此目录下有许多子目录,’grep’会以如下形式列出:
grep: sound: Is a directory
这可能会使’grep’ 的输出难于阅读。这里有两种解决的办法:
明确要求搜索子目录:grep -r
或忽略子目录:grep -d skip
如果有很多 输出时,可以通过管道将其转到’less’上阅读:
$ grep magic /usr/src/Linux/Documentation/* | less
这样,就可以更方便地阅读。

有一点要注意,必需提供一个文件过滤方式(搜索全部文件的话用 *)。如果忘了,’grep’会一直等着,直到该程序被中断。如果遇到了这样的情况,按 <CTRL c> ,然后再试。

下面还有一些有意思的命令行参数:
grep -i pattern files :不区分大小写地搜索。默认情况区分大小写,
grep -l pattern files :只列出匹配的文件名,
grep -L pattern files :列出不匹配的文件名,
grep -w pattern files :只匹配整个单词,而不是字符串的一部分(如匹配’magic’,而不是’magical’),
grep -C number pattern files :匹配的上下文分别显示[number]行,
grep pattern1 | pattern2 files :显示匹配 pattern1 或 pattern2 的行,
grep pattern1 files | grep pattern2 :显示既匹配 pattern1 又匹配 pattern2 的行。

grep -n pattern files  即可显示行号信息

grep -c pattern files  即可查找总行数

这里还有些用于搜索的特殊符号:
\< 和 \> 分别标注单词的开始与结尾。
例如:
grep man * 会匹配 ‘Batman’、’manic’、’man’等,
grep ‘\<man’ * 匹配’manic’和’man’,但不是’Batman’,
grep ‘\<man\>’ 只匹配’man’,而不是’Batman’或’manic’等其他的字符串。
‘^’:指匹配的字符串在行首,
‘$’:指匹配的字符串在行 尾,

举例
# ps -ef | grep in.telnetd
root 19955 181 0 13:43:53 ? 0:00 in.telnetd

# more size.txt size文件的内容
b124230
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
b103303
a013386
b044525
m8987131
B081016
M45678
B103303
BADc2345

# more size.txt | grep '[a-b]' 范围 ;如[A-Z]即A,B,C一直到Z都符合要求
b124230
b034325
a081016
a022021
a061048
b103303
a013386
b044525
# more size.txt | grep '[a-b]'*
b124230
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
b103303
a013386
b044525
m8987131
B081016
M45678
B103303
BADc2345

# more size.txt | grep 'b' 单个字符;如[A] 即A符合要求
b124230
b034325
b103303
b044525
# more size.txt | grep '[bB]'
b124230
b034325
b103303
b044525
B081016
B103303
BADc2345

# grep 'root' /etc/group
root::0:root
bin::2:root,bin,daemon
sys::3:root,bin,sys,adm
adm::4:root,adm,daemon
uucp::5:root,uucp
mail::6:root
tty::7:root,tty,adm
lp::8:root,lp,adm
nuucp::9:root,nuucp
daemon::12:root,daemon

# grep '^root' /etc/group 匹配正则表达式的开始行
root::0:root

# grep 'uucp' /etc/group
uucp::5:root,uucp
nuucp::9:root,nuucp

# grep '\<uucp' /etc/group
uucp::5:root,uucp

# grep 'root$' /etc/group 匹配正则表达式的结束行
root::0:root
mail::6:root

# more size.txt | grep -i 'b1..*3' -i :忽略大小写

b124230
b103303
B103303

# more size.txt | grep -iv 'b1..*3' -v :查找不包含匹配项的行

b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
a013386
b044525
m8987131
B081016
M45678
BADc2345

# more size.txt | grep -in 'b1..*3'
1:b124230
9:b103303
15:B103303

# grep '$' /etc/init.d/nfs.server | wc -l
128
# grep '\$' /etc/init.d/nfs.server | wc –l 忽略正则表达式中特殊字符的原有含义

15
# grep '\$' /etc/init.d/nfs.server
case "$1" in
> /tmp/sharetab.$$
[ "x$fstype" != xnfs ] &&
echo "$path\t$res\t$fstype\t$opts\t$desc"
>> /tmp/sharetab.$$
/usr/bin/touch -r /etc/dfs/sharetab /tmp/sharetab.$$
/usr/bin/mv -f /tmp/sharetab.$$ /etc/dfs/sharetab
if [ -f /etc/dfs/dfstab ] && /usr/bin/egrep -v '^[ ]*(#|$)'
if [ $startnfsd -eq 0 -a -f /etc/rmmount.conf ] &&
if [ $startnfsd -ne 0 ]; then
elif [ ! -n "$_INIT_RUN_LEVEL" ]; then
while [ $wtime -gt 0 ]; do
wtime=`expr $wtime - 1`
if [ $wtime -eq 0 ]; then
echo "Usage: $0 { start | stop }"

# more size.txt

the test file
their are files
The end

# grep 'the' size.txt
the test file
their are files

# grep '\<the' size.txt
the test file
their are files

# grep 'the\>' size.txt
the test file

# grep '\<the\>' size.txt
the test file

# grep '\<[Tt]he\>' size.txt
the test file


0 0
原创粉丝点击