首页 技术 正文
技术 2022年11月14日
0 收藏 829 点赞 3,040 浏览 9949 个字

转自:http://blog.csdn.net/hellochenlian/article/details/34088179

grep用法详解:grep与正则表达式首先要记住的是: 正则表达式与通配符不一样,它们表示的含义并不相同!
正则表达式只是一种表示法,只要工具支持这种表示法,那么该工具就可以处理正则表达式的字符串。vi grep ,awk ,sed 等都支持正则表达式.1基础正则表达式
grep 工具,以前介绍过。
grep -[acinv] '搜索内容串' filename
-a 以文本文件方式搜索
-c 计算找到的符合行的次数
-i 忽略大小写
-n 顺便输出行号
-v 反向选择,即找 没有搜索字符串的行
其中搜索串可以是正则表达式!搜索有the的行,并输出行号
$grep -n 'the' regular_express.txt
搜索没有the的行,并输出行号
$grep -nv 'the' regular_express.txt 利用[]搜索集合字符
[] 表示其中的某一个字符 ,例如[ade] 表示a或d或e
woody@xiaoc:~/tmp$ grep -n 't[ae]st' regular_express.txt
:I can't finish the test.
:Oh! the soup taste good!可以用^符号做[]内的前缀,表示除[]内的字符之外的字符。
比如搜索oo前没有g的字符串所在的行. 使用 '[^g]oo' 作搜索字符串
woody@xiaoc:~/tmp$ grep -n '[^g]oo' regular_express.txt
:apple is my favorite food.
:Football game is not use feet only.
:google is the best tools for search keyword.
:Goooooogle yes![] 内可以用范围表示,比如[a-z] 表示小写字母,[-] 表示0~9的数字, [A-Z] 则是大写字母们。[a-zA-Z0-]表示所有数字与英文字符。 当然也可以配合^来排除字符。
搜索包含数字的行
woody@xiaoc:~/tmp$ grep -n '[0-9]' regular_express.txt
:However ,this dress is about $ dollars.
:You are the best is menu you are the no..行首与行尾字符 ^ $. ^ 表示行的开头,$表示行的结尾( 不是字符,是位置)那么‘^$’ 就表示空行,因为只有
行首和行尾。
这里^与[]里面使用的^意义不同。它表示^后面的串是在行的开头。
比如搜索the在开头的行
woody@xiaoc:~/tmp$ grep -n '^the' regular_express.txt
:the symbol '*' is represented as star.搜索以小写字母开头的行
woody@xiaoc:~/tmp$ grep -n '^[a-z]' regular_express.txt
:apple is my favorite food.
:this dress doesn't fit me.
:motorcycle is cheap than car.
:the symbol '*' is represented as star.
:google is the best tools for search keyword.
:goooooogle yes!
:go! go! Let's go.
woody@xiaoc:~/tmp$ 搜索开头不是英文字母的行
woody@xiaoc:~/tmp$ grep -n '^[^a-zA-Z]' regular_express.txt
:"Open Source" is a good mechanism to develop programs.
:#I am VBird
woody@xiaoc:~/tmp$ $表示它前面的串是在行的结尾,比如 '\.' 表示 . 在一行的结尾
搜索末尾是.的行
woody@xiaoc:~/tmp$ grep -n '\.$' regular_express.txt //. 是正则表达式的特殊符号,所以要用\转义
:"Open Source" is a good mechanism to develop programs.
:apple is my favorite food.
:Football game is not use feet only.
:this dress doesn't fit me.
:However ,this dress is about $ dollars.
:GNU is free air not free beer.
.....注意在MS的系统下生成的文本文件,换行会加上一个 ^M 字符。所以最后的字符会是隐藏的^M ,在处理Windows
下面的文本时要特别注意!
可以用cat dos_file | tr -d '\r' > unix_file 来删除^M符号。 ^M==\r那么'^$' 就表示只有行首行尾的空行拉!
搜索空行
woody@xiaoc:~/tmp$ grep -n '^$' regular_express.txt
:
:
woody@xiaoc:~/tmp$ 搜索非空行
woody@xiaoc:~/tmp$ grep -vn '^$' regular_express.txt
:"Open Source" is a good mechanism to develop programs.
:apple is my favorite food.
:Football game is not use feet only.
:this dress doesn't fit me.
..........任意一个字符. 与重复字符 *在bash中*代表通配符,用来代表任意个字符,但是在正则表达式中,他含义不同,*表示有0个或多个 某个字符。
例如 oo*, 表示第一个o一定存在,第二个o可以有一个或多个,也可以没有,因此代表至少一个o.点. 代表一个任意字符,必须存在。 g??d 可以用 'g..d' 表示。 good ,gxxd ,gabd .....都符合。woody@xiaoc:~/tmp$ grep -n 'g..d' regular_express.txt
:"Open Source" is a good mechanism to develop programs.
:Oh! the soup taste good!
:The world is the same with 'glad'.
woody@xiaoc:~/tmp$ 搜索两个o以上的字符串
woody@xiaoc:~/tmp$ grep -n 'ooo*' regular_express.txt //前两个o一定存在,第三个o可没有,也可有多个。
:"Open Source" is a good mechanism to develop programs.
:apple is my favorite food.
:Football game is not use feet only.
:Oh! the soup taste good!
:google is the best tools for search keyword.
:goooooogle yes!搜索g开头和结尾,中间是至少一个o的字符串,即gog, goog....gooog...等
woody@xiaoc:~/tmp$ grep -n 'goo*g' regular_express.txt
:google is the best tools for search keyword.
:goooooogle yes!搜索g开头和结尾的字符串在的行
woody@xiaoc:~/tmp$ grep -n 'g.*g' regular_express.txt // .*表示 0个或多个任意字符
:"Open Source" is a good mechanism to develop programs.
:The gd software is a library for drafting programs.
:google is the best tools for search keyword.
:goooooogle yes!
:go! go! Let's go.限定连续重复字符的范围 { }
. * 只能限制0个或多个, 如果要确切的限制字符重复数量,就用{范围} 。范围是数字用,隔开 , 表示2~5个,
2表示2个,, 表示2到更多个
注意,由于{ }在SHELL中有特殊意义,因此作为正则表达式用的时候要用\转义一下。搜索包含两个o的字符串的行。
woody@xiaoc:~/tmp$ grep -n 'o\{2\}' regular_express.txt
:"Open Source" is a good mechanism to develop programs.
:apple is my favorite food.
:Football game is not use feet only.
:Oh! the soup taste good!
:google is the best tools for search keyword.
:goooooogle yes!搜索g后面跟2~5个o,后面再跟一个g的字符串的行。
woody@xiaoc:~/tmp$ grep -n 'go\{2,5\}g' regular_express.txt
:google is the best tools for search keyword.搜索包含g后面跟2个以上o,后面再跟g的行。。
woody@xiaoc:~/tmp$ grep -n 'go\{2,\}g' regular_express.txt
:google is the best tools for search keyword.
:goooooogle yes!注意,相让[]中的^ - 不表现特殊意义,可以放在[]里面内容的后面。
'[^a-z\.!^ -]' 表示没有小写字母,没有. 没有!, 没有空格,没有- 的 串,注意[]里面有个小空格。另外shell 里面的反向选择为[!range], 正则里面是 [^range]2扩展正则表达式扩展正则表达式是对基础正则表达式添加了几个特殊构成的。
它令某些操作更加方便。
比如我们要去除 空白行和行首为 #的行, 会这样用:
woody@xiaoc:~/tmp$ grep -v '^$' regular_express.txt | grep -v '^#'
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
............然而使用支持扩展正则表达式的 egrep 与扩展特殊符号 | ,会方便许多。
注意grep只支持基础表达式, 而egrep 支持扩展的,其实 egrep 是 grep -E 的别名而已。因此grep -E 支持扩展正则。
那么:
woody@xiaoc:~/tmp$ egrep -v '^$|^#' regular_express.txt
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
....................
这里| 表示或的关系。 即满足 ^$ 或者 ^# 的字符串。这里列出几个扩展特殊符号:
+,于 . * 作用类似,表示 一个或多个重复字符。
?, 于 . * 作用类似,表示0个或一个字符。
|,表示或关系,比如 'gd|good|dog' 表示有gd,good或dog的串
(),将部分内容合成一个单元组。比如 要搜索 glad 或 good 可以这样 'g(la|oo)d'
()的好处是可以对小组使用 + ? * 等。
比如要搜索A和C开头结尾,中间有至少一个(xyz) 的串,可以这样 : 'A(xyz)+C'◎grep -- print lines matching a pattern (将符合样式的该行列出) ◎语法: grep [options] PATTERN [FILE...] grep用以在file内文中比对相对应的部分,或是当没有指定档案时, 由标准输入中去比对。在预设的情况下,grep会将符合样式的那一行列出。 此外,还有两个程式是grep的变化型,egrep及fgrep。 其中egrep就等同於grep -E ,fgrep等同於grep -F 。 ◎参数 . -A NUM,--after-context=NUM 除了列出符合行之外,并且列出後NUM行。 ex: $ grep -A panda file (从file中搜寻有panda样式的行,并显示该行的後1行) . -a或--text grep原本是搜寻文字档,若拿二进位的档案作为搜寻的目标, 则会显示如下的讯息: Binary file 二进位档名 matches 然後结束。 若加上-a参数则可将二进位档案视为文字档案搜寻, 相当於--binary-files=text这个参数。 ex: (从二进位档案mv中去搜寻panda样式) (错误!!!) $ grep panda mv Binary file mv matches (这表示此档案有match之处,详见--binary-files=TYPE ) $ (正确!!!) $ grep -a panda mv . -B NUM,--before-context=NUM 与 -A NUM 相对,但这此参数是显示除符合行之外 并显示在它之前的NUM行。 ex: (从file中搜寻有panda样式的行,并显示该行的前1行) $ grep -B panda file . -C [NUM], -NUM, --context[=NUM] 列出符合行之外并列出上下各NUM行,预设值是2。 ex: (列出file中除包含panda样式的行外并列出其上下2行) (若要改变预设值,直接改变NUM即可) $ grep -C[NUM] panda file . -b, --byte-offset 列出样式之前的内文总共有多少byte .. ex: $ grep -b panda file 显示结果类似於: :panda :pandahuang :panda03 . --binary-files=TYPE 此参数TYPE预设为binary(二进位),若以普通方式搜寻,只有2种结果: .若有符合的地方:显示Binary file 二进位档名 matches .若没有符合的地方:什麽都没有显示。 若TYPE为without-match,遇到此参数, grep会认为此二进位档案没有包含任何搜寻样式,与-I 参数相同。 若TPYE为text, grep会将此二进位档视为text档案,与-a 参数相同。 Warning: --binary-files=text 若输出为终端机,可能会产生一些不必要的输出。 . -c, --count 不显示符合样式行,只显示符合的总行数。 若再加上-v,--invert-match,参数显示不符合的总行数。 . -d ACTION, --directories=ACTION 若输入的档案是一个资料夹,使用ACTION去处理这个资料夹。 预设ACTION是read(读取),也就是说此资料夹会被视为一般的档案; 若ACTION是skip(略过),资料夹会被grep略过: 若ACTION是recurse(递),grep会去读取资料夹下所有的档案, 此相当於-r 参数。 . -E, --extended-regexp 采用规则表示式去解释样式。 . -e PATTERN, --regexp=PATTERN 把样式做为一个partern,通常用在避免partern用-开始。 . -f FILE, --file=FILE 事先将要搜寻的样式写入到一个档案,一行一个样式。 然後采用档案搜寻。 空的档案表示没有要搜寻的样式,因此也就不会有任何符合。 ex: (newfile为搜寻样式档) $grep -f newfile file . -G, --basic-regexp 将样式视为基本的规则表示式解释。(此为预设) . -H, --with-filename 在每个符合样式行前加上符合的档案名称,若有路径会显示路径。 ex: (在file与testfile中搜寻panda样式) $grep -H panda file ./testfile file:panda ./testfile:panda $ . -h, --no-filename 与-H参数相类似,但在输出时不显示路径。 . --help 产生简短的help讯息。 . -I grep会强制认为此二进位档案没有包含任何搜寻样式, 与--binary-files=without-match参数相同。 ex: $ grep -I panda mv . -i, --ignore-case 忽略大小写,包含要搜寻的样式及被搜寻的档案。 ex: $ grep -i panda mv . -L, --files-without-match 不显示平常一般的输出结果,反而显示出没有符合的档案名称。 . -l, --files-with-matches 不显示平常一般的输出结果,只显示符合的档案名称。 . --mmap 如果可能,使用mmap系统呼叫去读取输入,而不是预设的read系统呼叫。 在某些状况,--mmap 能产生较好的效能。然而,--mmap 如果运作中档案缩短,或I/O 错误发生时, 可能造成未定义的行为(包含core dump),。 . -n, --line-number 在显示行前,标上行号。 ex: $ grep -n panda file 显示结果相似於下: 行号:符合行的内容 . -q, --quiet, --silent 不显示任何的一般输出。请参阅-s或--no-messages . -r, --recursive 递地,读取每个资料夹下的所有档案,此相当於 -d recsuse 参数。 . -s, --no-messages 不显示关於不存在或无法读取的错误讯息。 小: 不像GNU grep,传统的grep不符合POSIX.2协定, 因为缺乏-q参数,且他的-s 参数表现像GNU grep的 -q 参数。 Shell Script倾向将传统的grep移植,避开-q及-s参数, 且将输出限制到/dev/null。POSIX: 定义UNIX及UNIX-like系统需要提供的功能。 . -V, --version 显示出grep的版本号到标准错误。 当您在回报有关grep的bugs时,grep版本号是必须要包含在内的。 . -v, --invert-match 显示除搜寻样式行之外的全部。 . -w, --word-regexp 将搜寻样式视为一个字去搜寻,完全符合该"字"的行才会被列出。 . -x, --line-regexpgrep参数. -c 显示匹配的行数(就是显示有多少行匹配了);. -n 显示匹配内容所在文档的行号;. -i 匹配时忽略大小写;. -s 错误信息不输出;. -v 输出不匹配内容;. -x 输出完全匹配内容;. \ 忽略表达式中字符原有含义;. ^ 匹配表达式的开始行;. $ 匹配表达式的结束行;. \< 从匹配表达式的行开始;. \> 到匹配表达式的行结束;. [ ] 单个字符(如[A] 即A符合要求);. [ - ] 范围;如[A-Z]即A,B,C一直到Z都符合要求;. . 所有的单个字符;. * 所有字符,长度可以为0;[精华] Grep 用法Grep : g (globally) search for a re (regular expression ) and p (print ) the results. 、参数:
-I :忽略大小写
-c :打印匹配的行数
-l :从多个文件中查找包含匹配项
-v :查找不包含匹配项的行
-n:打印包含匹配项的行和行标 、RE(正则表达式)
\ 忽略正则表达式中特殊字符的原有含义
^ 匹配正则表达式的开始行
$ 匹配正则表达式的结束行
\< 从匹配正则表达式的行开始
\>; 到匹配正则表达式的行结束
[ ] 单个字符;如[A] 即A符合要求
[ - ] 范围 ;如[A-Z]即A,B,C一直到Z都符合要求
. 所有的单个字符
* 所有字符,长度可以为0 、举例
# ps -ef | grep in.telnetd
root :: ? : 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:::root
bin:::root,bin,daemon
sys:::root,bin,sys,adm
adm:::root,adm,daemon
uucp:::root,uucp
mail:::root
tty:::root,tty,adm
lp:::root,lp,adm
nuucp:::root,nuucp
daemon:::root,daemon # grep '^root' /etc/group 匹配正则表达式的开始行
root:::root # grep 'uucp' /etc/group
uucp:::root,uucp
nuucp:::root,nuucp # grep '\<uucp' /etc/group
uucp:::root,uucp # grep 'root$' /etc/group 匹配正则表达式的结束行
root:::root
mail:::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'
:b124230
:b103303
:B103303 # grep '$' /etc/init.d/nfs.server | wc -l # grep '\$' /etc/init.d/nfs.server | wc –l 忽略正则表达式中特殊字符的原有含义 # 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 -a -f /etc/rmmount.conf ] && \
if [ $startnfsd -ne ]; then
elif [ ! -n "$_INIT_RUN_LEVEL" ]; then
while [ $wtime -gt ]; do
wtime=`expr $wtime - `
if [ $wtime -eq ]; 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
The end何为转义:将特殊符号当普通符号来处理笔记:.^在[]内外的含义.何时需要转义.*在bash中和正则表达式中本身的区别.-acinv2014年6月24日11::
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,965
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781