grep 命令中表达式的应用。正则表达式在我们编程语言和linux系统中得到了很广泛的应用,利用表达式可以更快更方便的去查找信息。
表达式1:^标记,^标记用于查找以该标记后面的文本为开始的行。例如:grep ^this * 就是说查找改目录下所有文件中以this为开头的行。
jack@Ubuntu:~/demo$ cat message.1
this is a test
this is a demo
i love you
ihateyou
jack@Ubuntu:~/demo$ cat message.2
have a good day
this girl is my gf
that is not my love
jack@Ubuntu:~/demo$ grep "^this" *
message.1:this is a test
message.1:this is a demo
message.2:this girl is my gf
加上 -c 参数就是统计包含该文本的数量了
jack@Ubuntu:~/demo$ grep -c "^this" *
dirH:0
Hi.class:0
Hi.java:0
luch.txt:0
message.1:2
message.2:1
表达式2:$标记.该标记与^标记正好相反。旨在查找以该标记结尾的行。例如:grep "you$" message.1 是说查找message.1文件中以“you”结尾的行。
jack@Ubuntu:~/demo$ grep "you$" message.1
i love you
ihateyou
表达式3:整合^$标记.单纯把^$放在一起是查找空行。
jack@Ubuntu:~/demo$ cat message.3
this is for test empty line
one empty line above
two empty lines above.
jack@Ubuntu:~/demo$ grep "^$" message.3
jack@Ubuntu:~/demo$ grep -c "^$" messag*
message.1:0
message.2:0
message.3:3
表达式4:单点.标记。单点标记用来匹配含有其中任意字符的行。例如:grep “i.love” * 就是匹配含有i和love的行,但是不匹配ilove。
jack@Ubuntu:~/demo$ cat message.1
this is a test
this is a demo
i love you
iloveyou
ihateyou
jack@Ubuntu:~/demo$ grep "i.love" message*
message.1:i love you
如果你是想匹配某个单词的话,加-w 参数。例如:
jack@Ubuntu:~/demo$ grep -w "you" message*
message.1:i love you
表达式5:*标记,该标记用于匹配零个或多个前面的关键字的行。
jack@Ubuntu:~/demo$ cat message.4
gle
google
gogle
jack@Ubuntu:~/demo$ grep "go*gle" message.4
google
gogle
jack@Ubuntu:~/demo$ grep "*gle" message.4
jack@Ubuntu:~/demo$ grep "g*gle" message.4
gle
google
gogle
这里情况有点特殊,对于0个的情况没有能查询欻里。但是用g*gle却能查询出来不是很了解具体的情况。我的系统是
Linux version 3.5.0-52-generic (buildd@klock) (gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) ) #78~precise1-Ubuntu SMP Wed Jun 11 17:14:53 UTC 2014
请高人指点
--EOF--
分享到:
相关推荐
grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。Unix的grep家族包括grep...
Regular expression experts have long been armed with an incredibly powerful tool, one that can be used to perform all sorts of sophisticated text processing and manipulation in just about every ...
grub.pdf grep.pdf (2 pieces of PDF Inc.)
Regular expression experts have long been armed with an incredibly powerful tool, one that can be used to perform all sorts of sophisticated text processing and manipulation in just about every ...
离线安装包,亲测可用
grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户。 2. grep的工作方式是这样的,它在一个或多个文件中搜索字符串模板。如果模板包括空格,则必须被引用,模板后的所有...
Regular expression 具有可以表达出难以描述、複杂、但是却有特殊规则的字串的功能,所以许多的 UNIX 工具程式都有支援 Regular expression 的功能。例如 ex 、 vi 、 sed 、 awk 、 grep 、 emacs 等等都有支援。...
大数据基础-Linux基础详解课程
grep-2.20-3.el7.x86_64.rpm
Conventions Used in This Book......................................................................................... 6 How to Contact Us ................................................................
grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。Unix的grep家族包括grep、 ...
grep-2.5.4-setup windows的grep最新版,安装后可在windows操作系统上像在linux上使用grep命令。
windows下的grep命令,解压缩后将grep.exe放在C:\Windows下即可使用,或者手动将grep.exe所在的路径加入到PATH环境变量中也可以
windows grep包 解压后,grep路径加入PATH,即可使用 D:\>grep Usage: grep [OPTION]... PATTERN [FILE]... Try 'grep --help' for more information.
掌握:使用clojure.spec regexes的Grep Clojure代码
Test: If a regular expression contained an inverted range with a \xFF escape and the active flavor does not support inverted ranges then RegexBuddy would continue to complain about the inverted range...
使用 debtap 重新对 mindmaster 8.0.3 打包。可以登录帐号和正常切换中文输入法。 mindmaster-8.0.3-1-x86_64.pkg.tar.zst
这些都是linux下面的命令,EXE文件可以在windows系统里使用。
导入源码jar学习 但是这种方式项目还没有准备好构建,并且它缺少一些外部代码: 缺少name.schedenig.eclipse.popupnotifications包/插件依赖项, 例如对于name.schedenig.eclipse.popupnotifications....
grep (缩写来自Globally search a Regular Expression and Print)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。Unix的grep家族包括grep、egrep和fgrep