盗版2 grep用法大全

来源:互联网 发布:windows xp纯净版系统 编辑:程序博客网 时间:2024/05/22 15:59
Grep : g (globally) search for a re (regular expression ) and p (print ) the results.

1、参数:
-I :忽略大小写
-c :打印匹配的行数
-l :从多个文件中查找包含匹配项
-v :查找不包含匹配项的行
-n:打印包含匹配项的行和行标

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

3、举例
# 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 '
' 单个字符;如[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
1 双引号引用
在grep命令中输入字符串参数时,最好将其用双引号括起来
2 grep选项
常用的g r e p选项有:
-c 只输出匹配行的计数。
-i 不区分大小写(只适用于单字符)。
-h 查询多文档时不显示文档名。
-l 查询多文档时只输出包含匹配字符的文档名。
-n 显示匹配行及行号。
-s 不显示不存在或无匹配文本的错误信息。
-v 显示不包含匹配文本的任何行。
3 精确匹配
使用grep抽取精确匹配的一种更有效方式是在抽取字符串后加/>。假定现在精确抽取48,则为"48/>"
三 grep和正则表达式
使用正则表达式时最好用单引号括起来,这样能够防止grep中使用的专有模式和一些shell命令的特别方式相混淆。
1 模式范围
假定要抽取代码为484和483的城市位置,上一章中讲到能够使用[ ]来指定字符串范围,这里用48开始,
以3或4结尾,这样抽出484或483。grep '48[34]' data.f
2 不匹配行首
假如要抽出记录,使其行首不是48,能够在方括号中使用^记号,表明查询在行首开始。
grep '^[^48]' data.f
3 匹配任意字符
假如抽取以L开头,以D结尾的任何代码,可使用下述方法,因为已知代码长度为5个字符:
grep 'L...D' data.f
4 日期查询
一个常用的查询模式是日期查询。先查询任何以5开始以1 9 9 6或1 9 9 8结尾的任何记录。使用模式5..199[6,8].这意味着第一个字符为
5,后跟两个点,接着是199,剩余两个数字是6或8。
grep '5..199[6,8]' data.f
查询包含1998的任何记录的另外一种方法是使用表达式[0-9]/[8],含义是任意数字重复3次,后跟数字8,虽然这个方法不像上一个方
法那么精确,但也有一定作用。
5 使用grep匹配“和”或“或”模式
grep命令加- E参数,这一扩展允许使用扩展模式匹配。例如,要抽取城市代码为2 1 9或2 1 6,方法如下:
grep -E '219|216' data.f
6 空行
结合使用^和$可查询空行。使用- n参数显示实际行数:
grep '^$' data.f
7 grep 和类名的使用
类等价的正则表达式类等价的正则表达式
[[:upper:]] [A-Z] [[:alnum:]] [0-9a-zA-Z]
[[:lower:]] [a-z] [[:space:]] 空格或t a b键
[[:digit:]] [0-9] [[:alpha:]] [a-zA-Z]


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wanjichun/archive/2008/11/14/3297386.aspx
 
 
 
 
 
使用grep搜索文件内容——快捷、方便

(1) 在当前目录下的所有文件的文件内容中查找哪个文件的内容中有findcontents(大小写不敏感,列出findcontents
所在文件的所在行)——适合于当前目录下的文件及目录数目比较少,如果查找后列出内容过多,将会失去查找意义。
grep -rin findcontents *

(2) 如果查找后列出内容过多,就需要用如下命令(只列出findcontents在所在文件的出现次数):
grep -ric findcontents * | grep -v :0

(3) 如果根本不知道文件所在目录,就需要在根目录下进行查找(查找整个硬盘空间),但是由于根目录下某些文件(bin、sbin、boot、dev、initrd、lib)没有查找价值,因而要将其排除在查找范围之外
# cd /
grep -rin findcontents `ls | grep -vE 'bin|boot|dev|initrd|lib'`

------------------------------------------

-i, --ignore-case
-n, --line-number
-c, --count
-r, --recursive
-v, --invert-match
-E, --extended-regexp
-e PATTERN, --regexp=PATTERN
Use PATTERN as the pattern; useful to protect patterns beginning with -.
注:有c的时候n将失效

 

 

 

 

 

 

4.2. 使用grep的例子

4.2.1. 什么是grep?

grep 以行为单位搜索那些包含给出模板列表的输入文件。当在一行中找到匹配,默认把该行拷贝到标准输出(默认),或者其他你以选项要求的任何种类的输出。 searches the input files for lines containing a match to a given pattern list. When it finds a match in a line, it copies the line to standard output (by default), or whatever other sort of output you have requested with options.

尽管 grep 希望在基于文本进行匹配,除了现有的内存它对于输入行的长度没有限制,而且它可以匹配某行的任意字符。如果输入文件的最后一个字节不是一个 换行grep 就悄悄增加一个。既然换行也是一个模板列表的分隔符,那么就没有办法在一个文本中匹配一个换行字符。

一些例子:

DE>cathy ~>DE> grep DE>rootDE> DE>/etc/passwdDE>root:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologinDE>cathy ~>DE> grep DE>-nDE> DE>rootDE> DE>/etc/passwdDE>1:root:x:0:0:root:/root:/bin/bash12:operator:x:11:0:operator:/root:/sbin/nologinDE>cathy ~>DE> grep DE>-vDE> DE>bashDE> DE>/etc/passwd DE>| grep DE>-vDE> DE>nologinDE>sync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltnews:x:9:13:news:/var/spool/news:mailnull:x:47:47::/var/spool/mqueue:/dev/nullxfs:x:43:43:X Font Server:/etc/X11/fs:/bin/falserpc:x:32:32:Portmapper RPC user:/:/bin/falsenscd:x:28:28:NSCD Daemon:/:/bin/falsenamed:x:25:25:Named:/var/named:/bin/falsesquid:x:23:23::/var/spool/squid:/dev/nullldap:x:55:55:LDAP User:/var/lib/ldap:/bin/falseapache:x:48:48:Apache:/var/www:/bin/falseDE>cathy ~>DE> grep DE>-cDE> DE>falseDE> DE>/etc/passwdDE>7DE>cathy ~>DE> grep DE>-iDE> DE>psDE> DE>~/.bash*DE> | grep DE>-vDE> DE>historyDE>/home/cathy/.bashrc:PS1="/[/033[1;44m/]$USER is in /w/[/033[0m/] "

第一个例子,用户 cathy 把 DE>/etc/passwdDE> 里面包含 root字符串的行显示出来。

然后她显示了包含着个搜索字符串的行的号码。

用第三个命令她检查了哪个用户没有使用 bash,但是使用 nologin shell的账号不会显示。

然后她统计了把 DE>/bin/falseDE> 作为shell的账户的数量。

最后的命令显示了在她的home目录中所有包含以 DE>~/.bashDE> 开头的行的文件,excluding matches containing history, so as to exclude matches from DE>~/.bash_historyDE> which might contain the same string, in upper or lower cases.

现在让我们来看看用正则表达式,grep还能做些什么。

4.2.2. Grep与正则表达式

[注意]如果你不是在使用Linux

我们在这些例子中使用支持扩展正则表达式的GNU grep。GNU grep 在Linux系统里是默认的。如果你在专有的系统上工作,那么请使用 DE>-VDE> 选项来检查你在使用哪个版本的grep。GNU grep 也可以从 http://gnu.org/directory/ 下载。

4.2.2.1. 锚定行和字

从先前的例子中,我们只想显示那些使用字符串 “root” 开头的行:

DE>cathy ~>DE> grep DE>^rootDE> DE>/etc/passwdDE>root:x:0:0:root:/root:/bin/bash

如果我们想看哪个账号什么shell都没有分配,我们搜索行结束符 “:”:

DE>cathy ~>DE> grep DE>:$DE> DE>/etc/passwdDE>news:x:9:13:news:/var/spool/news:

要检查 DE>PATHDE> 是否在 DE>~/.bashrcDE> 中导出,首先选择 “export” 然后搜索以字符串 DE>PATHDE> 开始的行,这样就不会搜索到MANPATH或者其他可能的路径了:

DE>cathy ~>DE> grep DE>exportDE> DE>~/.bashrcDE> | grep DE>'/<PATH'DE>  export PATH="/bin:/usr/lib/mh:/lib:/usr/bin:/usr/local/bin:/usr/ucb:/usr/dbin:$PATH"

同样地,/> 匹配字的结束。

如果你想找到是一个分隔的字的字符串(用空格来包围的),最好使用 DE>-wDE>,就像在这个例子中我们现实root分区的信息那样:

DE>cathy ~>DE> grep DE>-wDE> DE>/DE> DE>/etc/fstabDE>LABEL=/                 /                       ext3    defaults        1 1

如果不使用这个选项,系统表中的所有行都会被显示。

4.2.2.2. 字符族Character classes

方括号表达式 是一个用 “[” and “]” 包含起来的字符列表。它匹配任何在列表中的单个字符;如果列表的第一个字符是 “^”,那么它匹配任何不在列表中的字符。比如,正则表达式,“[0123456789]” 匹配任何单个数字。

在一个方括号表达式中,范围表达式range expression 由2个横线分隔的字符组成。它匹配任何排列在2个字符之间的单个字符,包括,使用场合比较序列和字符集。比如,默认的C场合,“[a-d]” 等于 “[abcd]”。许多场合以字典的顺序来对字符进行分类,在这些场合中 “[a-d]” 通常不等于 “[abcd]”;可能等于 “[aBbCcDd]”。比如,为了得到对于方括号表达式的惯用解释,你可以通过设置 DE>LC_ALLDE> 环境变量的值为 “C” 来使用C场合。

最后,特定的命名的字符族是在括号内事先定义的。参见 grep man 或者 info 页面得到更多关于预定义表达式的信息。

DE>cathy ~>DE> grep DE>[yf]DE> DE>/etc/groupDE>sys:x:3:root,bin,admtty:x:5:mail:x:12:mail,postfixftp:x:50:nobody:x:99:floppy:x:19:xfs:x:43:nfsnobody:x:65534:postfix:x:89:DE>cathy ~>DE> ls DE>*[1-9].xmlDE>app1.xml  chap1.xml  chap2.xml  chap3.xml  chap4.xml

在这个例子中,所有包含一个 “y” 或者 “f” 字符的行首先显示,之后是一个 ls 命令使用范围的例子。

4.2.2.3. 统配符

使用 “.” 进行一个单字符匹配。如果你想得到一个以 “c” 开头且以 “h” 结尾的所有5位字符的英语辞典词条(来解决 crosswords 游戏,就是那个纵横填字游戏):

DE>cathy ~>DE> grep DE>'/<c...h/>'DE> DE>/usr/share/dict/wordsDE>catchclashclothcoachcouchcoughcrashcrush

如果你想显示包含点字符的行,使用 DE>-FDE> 选项给 grep.

要匹配多个字符,使用星号。这个例子从系统的词典中选择了所有以 “c” 开头且以 “h” 结尾的单词:

DE>cathy ~>DE> grep DE>'/<c.*h/>'DE> DE>/usr/share/dict/wordsDE>caliphcashcatchcheeseclothcheetah--output omitted--

如果你想在一个文件或者输出中找到包含星号字符的行,使用 grep DE>-FDE>

DE>cathy ~>DE> grep DE>*DE> DE>/etc/profileDE>DE>cathy ~>DE> grep DE>-FDE> DE>'*'DE> DE>/etc/profileDE>for i in /etc/profile.d/*.sh ; do
来源:http://heshaoqin126.blog.163.com/blog/static/85557269200981410265141/

原创粉丝点击