grep、egrep、fgrep以及正则表达式学习总结
一、grep起源 二、grep用处 三、grep使用 PATTERN是一个普通字符串 PATTERN是一个正则表达式 基本正则表达式 扩展正则表达式 PS:为了能更好的学习每一个知识,我在每一个知识点后面都有加一个例子,自己多多动手,多多练习,这样效果才会更好。 一、grep起源grep原先是ed下的一个应用程序,其名称来自于g/re/p(globally search a regular expression and print,以正规表示法进行全域查找以及打印),在ed下,输入g/re/p这个命令后,会将所有符合原先定义的字符串,以行为单位打印出来 二、grep用处grep是用来搜索文本或者搜索某些文件里面的行,这些行里面包括和给定的字符串或者单词相匹配的字符串或者单词。默认情况下,grep显示出匹配到的行。用grep搜索文本,找到里面匹配一个或者多个正则表达式的行,并且输出这些行。Grep被认为是UNIX和其他类UNIX操作系统中最有用的命令之一。 三、grep使用
PATTERN是一个字符串:
grep常用选项:--color:
[root@localhostbash]#grep--color=auto'hi'b.txt hijerry -v:反向选取,以下面为例,就是不包含"hi"字符串[root@localhostbash]#catb.txt helloworld hijerry mynameislinux. [root@localhostbash]#grep-v'hi'b.txt helloworld mynameislinux. -o:仅显示匹配到的内容
[root@localhostbash]#grep'hi'b.txt hijerry [root@localhostbash]#grep-o'hi'b.txt hi -n:在查出来的每个行的前面加上每行在文件中的行号[root@localhostbash]#cat-n/etc/issue 1CentOSrelease6.5(Final) 2Kernelronanm 3 4MageEducationLearningServices 5http://www.magedu.com 6 [root@localhostbash]#grep-n'www'/etc/issue 5:http://www.magedu.com -i:忽略字符大小写,以下面为例,加上-i之后,能匹配到"My"了
[root@localhostbash]#catb.txt helloworld hijerry mynameislinux. Helloworld Mynameislinux. [root@localhostbash]#grep'my'b.txt mynameislinux. [root@localhostbash]#grep-i'my'b.txt mynameislinux. Mynameislinux. -A #:把匹配到的行的后#行也打印出来,以下面为例,把匹配到的那一行的后两行也打印出来;
[root@localhostbash]#catb.txt helloworld hijerry mynameislinux. Helloworld whatisyourname? Learnlinuxisfun. Doyouthinkso? [root@localhostbash]#grep-A2'what'b.txt whatisyourname? Learnlinuxisfun. Doyouthinkso? -B #:把匹配到的行的前#行业打印出来,以下面为例,把匹配到的那一行的前两行也打印出来;[root@localhostbash]#catb.txt helloworld hijerry mynameislinux. Helloworld whatisyourname? Learnlinuxisfun. Doyouthinkso? [root@localhostbash]#grep-B2'what'b.txt mynameislinux. Helloworld whatisyourname? -C #:把匹配到的行的前#行和后#行也打印出来,如下所示,把匹配到那行的前后两行都打印出来;
[root@localhostbash]#catb.txt helloworld hijerry mynameislinux. Helloworld whatisyourname? Learnlinuxisfun. Doyouthinkso? [root@localhostbash]#grep-C2'what'b.txt mynameislinux. Helloworld whatisyourname? Learnlinuxisfun. Doyouthinkso? 3.2、PATTERN是一个正则表达式:尽管直接使用最简单直接的pattern字串可以完成一些重要的任务,但是grep命令的真正的威力在于它可以使用正则表达式来完成复杂的模式字串的匹配。 3.2.1 什么是正则表达式?正则表达式:正则表达式又称正规表达式、正规表示法、正规表示式、规则表达式、常规表达式(英文:Regular Expression―>这里正好和grep的英文名对应,grep就是搜索正规表达式并且打印输出),在代码中常常简写为regex,regexp或RE,计算机科学的一个概念。正则表达式使用单个字符串来描述、匹配一系列符合某个句法规则的字符串。在很多文本编辑器里,正则表达式通常被用来检索、替换那些符合某个模式的文本。许多程序设计语言都支持利用正则表达式进行字符串操作。例如,在Perl中就内建了一个功能强大的正则表达式引擎。正则表达式这个概念最初是由UNIX中的工具软件(例如sed和grep)普及开的。正则表达式通常缩写成”regex”,单数有regexp、regex,复数有regexps、regexes、regexen。 正则表达式历史:最初的正则表达式出现于理论计算机科学的自动控制理论和形式化语言理论中。在这些领域中有对计算(自动控制)的模型和对形式化语言描述与分类的研究。 1940年,WarrenMcCulloch与Walter Pitts将神经系统中的神经元描述成小而简单的自动控制元。 1950年代,数学家斯蒂芬?科尔?克莱尼利用称之为“正则集合”的数学符号来描述此模型。肯?汤普逊将此符号系统引入编辑器QED,然后是Unix上的编辑器ed,并最终引入grep。自此,正则表达式被广泛地使用于各种Unix或者类似Unix的工具,例如Perl。 正则表达式两类
在grep的PATTERN中分两类正则表达式:
Grep命令中使用的是基本的正则表达式,如果想使用更高级也就是扩展正则表达式需要指定选项-E,相当于egrep命令。 基本正则表达式:包括四种类型的模式匹配规则字符匹配:
[root@localhostbash]#catb.txt helloworld hijerry mynameislinux. Helloworld whatisyourname? Learnlinuxisfun. Doyouthinkso? [root@localhostbash]#grep't...k'b.txt Doyouthinkso?
[root@localhostbash]#catb.txt helloworld hijerry mynameislinux. Helloworld whatisyourname? Learnlinuxisfun. Doyouthinkso? 10youthinkso? [root@localhostbash]#grep'[[:digit:]]'b.txt 10youthinkso? [root@localhostbash]#catb.txt helloworld hijerry mynameislinux. Helloworld whatisyourname? Learnlinuxisfun. Doyouthinkso? 10youthinkso? [root@localhostbash]#grep'[0-9]'b.txt 10youthinkso?
[root@localhostbash]#catb_test.txt HI,WHATISYOURNAME? MYNAMEISJERRY HOWOLDAREYOU? Iam22. [root@localhostbash]#grep'[[:lower:]]'b_test.txt Iam22. [root@localhostbash]#catb_test.txt HI,WHATISYOURNAME? MYNAMEISJERRY HOWOLDAREYOU? Iam22. [root@localhostbash]#grep'[a-z]'b_test.txt
[root@localhostbash]#catb.txt helloworld hijerry mynameislinux. Helloworld whatisyourname? Learnlinuxisfun. Doyouthinkso? 10youthinkso? [root@localhostbash]#grep'[[:upper:]]'b.txt Helloworld Learnlinuxisfun. Doyouthinkso? [root@localhostbash]#catb.txt helloworld hijerry mynameislinux. Helloworld whatisyourname? Learnlinuxisfun. Doyouthinkso? 10youthinkso? [root@localhostbash]#grep'[A-Z]'b.txt Helloworld Learnlinuxisfun. Doyouthinkso?
[root@localhostbash]#catb.txt helloworld hijerry mynameislinux. Helloworld whatisyourname? Learnlinuxisfun. Doyouthinkso? 10youthinkso? 100 [root@localhostbash]#grep-v'[[:alpha:]]'b.txt 100 [root@localhostbash]#catb.txt helloworld hijerry mynameislinux. Helloworld whatisyourname? Learnlinuxisfun. Doyouthinkso? 10youthinkso? 100 [root@localhostbash]#grep-v'[a-zA-Z]'b.txt 100
[root@localhostbash]#catc.txt howoldareyou? iam22 ***** [root@localhostbash]#grep'[[:alnum:]]'c.txt howoldareyou? iam22 [root@localhostbash]#catc.txt howoldareyou? iam22 ***** [root@localhostbash]#grep'[0-9a-zA-Z]'c.txt howoldareyou? iam22
[root@localhostbash]#catc.txt howoldareyou? iam22 ***** youyouyouyou [root@localhostbash]#grep'[[:space:]]'c.txt howoldareyou? iam22
[root@localhostbash]#catc.txt howoldareyou? iam22 ***** youyouyouyou mynameisjerry,andwhatisyourname? [root@localhostbash]#grep'[[:punct:]]'c.txt howoldareyou? ***** mynameisjerry,andwhatisyourname?
[root@localhostbash]#grep'[^0-9]'b.txt helloworld hijerry mynameislinux. Helloworld whatisyourname? Learnlinuxisfun. Doyouthinkso? 10youthinkso? [root@localhostbash]#catb.txt helloworld hijerry mynameislinux. Helloworld whatisyourname? Learnlinuxisfun. Doyouthinkso? 10youthinkso? 100 匹配次数:
[root@localhostbash]#catd.txt xy xxy xxxy xxxxy y nxy [root@localhostbash]#grep'x*y'd.txt xy xxy xxxy xxxxy y nxy [root@localhostbash]#grep'xx*y'd.txt xy xxy xxxy xxxxy nxy [root@localhostbash]#grep'xxx*y'd.txt xxy xxxy xxxxy
[root@localhostbash]#catd.txt xy xxy xxxy xxxxy y nxy [root@localhostbash]#grep'x?y'd.txt xy xxy xxxy xxxxy y nxy [root@localhostbash]#grep'xx?y'd.txt xy xxy xxxy xxxxy nxy
[root@localhostbash]#catd.txt xy xxy xxxy xxxxy y nxy [root@localhostbash]#grep'x+y'd.txt xy xxy xxxy xxxxy nxy [root@localhostbash]#grep'xx+y'd.txt xxy xxxy xxxxy [root@localhostbash]#grep'xxx+y'd.txt xxxy xxxxy
[root@localhostbash]#catd.txt xy xxy xxxy xxxxy y nxy [root@localhostbash]#grep'x{2}y'd.txt xxy xxxy xxxxy [root@localhostbash]#grep'x{1}y'd.txt xy xxy xxxy xxxxy nxy [root@localhostbash]#grep'x{3}y'd.txt xxxy xxxxy
[root@localhostbash]#catd.txt xy xxy xxxy xxxxy y nxy [root@localhostbash]#grep'x{1,2}y'd.txt xy xxy xxxy xxxxy nxy [root@localhostbash]#grep'x{2,4}y'd.txt xxy xxxy xxxxy [root@localhostbash]#grep'x{3,5}y'd.txt xxxy xxxxy
[root@localhostbash]#catd.txt xy xxy xxxy xxxxy y nxy [root@localhostbash]#grep'x{2,}y'd.txt xxy xxxy xxxxy [root@localhostbash]#grep'x{3,}y'd.txt xxxy xxxxy [root@localhostbash]#grep'x{4,}y'd.txt xxxxy [root@localhostbash]#grep'x{5,}y'd.txt [root@localhostbash]#
[root@localhostbash]#grep'r.*t'/etc/passwd root:x:0:0:Administrator,1456,741,963:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin ftp:x:14:50:FTPUser:/var/ftp:/sbin/nologin vcsa:x:69:69:virtualconsolememoryowner:/dev:/sbin/nologin rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin avahi-autoipd:x:170:170:AvahiIPv4LLStack:/var/lib/avahi-autoipd:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin saslauth:x:498:76:"Saslauthduser":/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separatedSSH:/var/empty/sshd:/sbin/nologin user6:x:4009:4009::/tmp/user6:/bin/bash user8:x:4010:4010::/home/user8:/bin/tcsh user21:x:4012:4012::/tmp/user21:/bin/bash gentoo:x:4021:4021::/users/gentoo:/bin/bash centos:x:4022:4022::/users/centos:/bin/bash 位置锚定:
[root@localhostbash]#grep'^r'/etc/passwd root:x:0:0:Administrator,963:/root:/bin/bash rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin rpc:x:32:32:RpcbindDaemon:/var/cache/rpcbind:/sbin/nologin rpcuser:x:29:29:RPCServiceUser:/var/lib/nfs:/sbin/nologin
[root@localhostbash]#grep'nologin$'/etc/passwd bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin ftp:x:14:50:FTPUser:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:Systemmessagebus:/:/sbin/nologin usbmuxd:x:113:113:usbmuxduser:/:/sbin/nologin vcsa:x:69:69:virtualconsolememoryowner:/dev:/sbin/nologin rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin rpc:x:32:32:RpcbindDaemon:/var/cache/rpcbind:/sbin/nologin avahi-autoipd:x:170:170:AvahiIPv4LLStack:/var/lib/avahi-autoipd:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin haldaemon:x:68:68:HALdaemon:/:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin apache:x:48:48:Apache:/var/www:/sbin/nologin saslauth:x:498:76:"Saslauthduser":/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin rpcuser:x:29:29:RPCServiceUser:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:AnonymousNFSUser:/var/lib/nfs:/sbin/nologin pulse:x:497:496:PulseAudioSystemDaemon:/var/run/pulse:/sbin/nologin sshd:x:74:74:Privilege-separatedSSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin pgsql:x:495:492::/home/pgsql:/sbin/nologin nologin:x:4017:4017::/home/nologin:/sbin/nologin mysql:x:4020:491::/home/mysql:/sbin/nologin test:x:4023:4023::/home/test:/sbin/nologin
[root@localhostbash]#cat-n/etc/issue 1CentOSrelease6.5(Final) 2Kernelronanm 3 4MageEducationLearningServices 5http://www.magedu.com 6 [root@localhostbash]#grep-n'^$'/etc/issue 3: 6:
[root@localhostbash]#catb.txt helloworld hijerry mynameislinux. Helloworld rootis whatisyourname? Learnlinuxisfun. Doyouthinkso? hasrootis 10youthinkso? 100 hyroothehe [root@localhostbash]#grep'root'b.txt rootis hasrootis hyroothehe [root@localhostbash]#grep'&;root'b.txt rootis hyroothehe
[root@localhostbash]#vimb.txt [root@localhostbash]#catb.txt helloworld hijerry mynameislinux. Helloworld rootis whatisyourname? Learnlinuxisfun. Doyouthinkso? hasrootis 10youthinkso? 100 hyroothehe [root@localhostbash]#grep'root'b.txt rootis hasrootis hyroothehe [root@localhostbash]#grep'root&;'b.txt rootis
[root@localhostbash]#catb.txt helloworld yourrootishhi hijerry mynameislinux. Helloworld rootis whatisyourname? Learnlinuxisfun. Doyouthinkso? hasrootis 10youthinkso? 100 hyroothehe [root@localhostbash]#grep'root'b.txt yourrootishhi rootis hasrootis hyroothehe [root@localhostbash]#grep'&;root&;'b.txt rootis
[root@localhostbash]#catn.tx higodll whatgooduuu lsdoweur oweu9roweur hhngodlii sdfsdfor afewgoodeee [root@localhostbash]#grep'god|good'n.tx higodll whatgooduuu hhngodlii afewgoodeee
[root@localhostbash]#catn.tx higodll whatgooduuu lsdoweur oweu9roweur ilovegold hhngodlii sdfsdfor afewgoodeee goodiswhat? [root@localhostbash]#grep'go(o|l)d'n.tx whatgooduuu ilovegold afewgoodeee goodiswhat? 后向引用:
[root@localhostbash]#catm.txt Ilikealiker Ilovealover Ilikealover Ilovealiker [root@localhostbash]#grep'l..e.*l..er'm.txt Ilikealiker Ilovealover Ilikealover Ilovealiker [root@localhostbash]#grep'(l..e).*1'm.txt Ilikealiker Ilovealover 扩展正则表达式 见名思义,可知是扩展的正则表达式,比基本正则表达式所支持的功能更多,在上面的基本正则表达式中大家可能最常见的就是""这个符号,这个符号就是转义字符,除了单词锚定的这个"&;&;"符号在扩展正则表达式不变,其余的如果想用在扩展的正则表达式上的话,去掉""就行了,其余的都不变,由于时间问题我在这里就不废话了,大家自己练习就行。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |