`

Usage of grep

 
阅读更多


1.作用
linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户。
2.格式
grep [options]
3.主要参数
[options]主要参数:
-c:只输出匹配行的计数。
-I:不区分大小写(只适用于单字符)。
-h:查询多文件时不显示文件名。
-l:查询多文件时只输出包含匹配字符的文件名。
-n:显示匹配行及行号。
-s:不显示不存在或无匹配文本的错误信息。
-v:显示不包含匹配文本的所有行。
pattern正则表达式主要参数:
\:忽略正则表达式中特殊字符的原有含义。
^:匹配正则表达式的开始行。
$: 匹配正则表达式的结束行。
\<:从匹配正则表达式的行开始。
\>:到匹配正则表达式的行结束。
[ ]:单个字符,如[A]即A符合要求 。
[ - ]:范围,如[A-Z],即A、B、C一直到Z都符合要求 。
。:所有的单个字符。
* :有字符,长度可以为0。
4.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′就可以了。
5.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 man * 会匹配 ‘Batman’、’manic’、’man’等,
grep ‘\<man’ * 匹配’manic’和’man’,但不是’Batman’,
grep ‘\<man\>’ 只匹配’man’,而不是’Batman’或’manic’等其他的字符串。
‘^’:指匹配的字符串在行首,
‘$’:指匹配的字符串在行尾,
原文地址: http://www.9usb.net/200902/linux-grep.html

分享到:
评论

相关推荐

    Linux Shell Scripting Cookbook.pdf

    Proper usage of shell commands can easily solve many complex tasks with a few lines of code, but most linux users don't have the right know-how to use the Linux shell to its full potential. ...

    Linux Shell Scripting Cookbook.epub

    Proper usage of shell commands can easily solve many complex tasks with a few lines of code, but most linux users don't have the right know-how to use the Linux shell to its full potential. ...

    Linux Shell Scripting Cookbook.mobi

    Proper usage of shell commands can easily solve many complex tasks with a few lines of code, but most linux users don't have the right know-how to use the Linux shell to its full potential. ...

    pgrep [Win]

    Usage: pgrep [OPTION]... PATTERN [FILE] ... &lt;If you run from source code&gt; python pgrep [OPTION]... PATTERN [FILE] ... Search for PATTERN in each FILE or standard input and output a paragraph ...

    bingrep:grep的一个小实用程序,用于在内存中转储指针和二进制数据,转储实时进程内存

    用法Exactly one of -f or -p has to be specified.Usage: 1) bingrep -f &lt;filename&gt; [pattern] 2) bingrep -p &lt;pid&gt; [-i ] [pattern]Pattern can be one of: -s &lt;from&gt; -e &lt;to&gt; [-w ] -b &lt;hex&gt; -a &lt;ascii&gt;For type ...

    gosec:Gosec使用PGP管理机密

    Usage of ./gosec: -d=false: Decrypt -e=false: Encrypt -g= " " : Regex String -s= " " : Directory Root directory must be specified 安装 GO15VENDOREXPERIMENT=1 godep get github.com/rphillips/gosec

    ora分析脚本

    Usage: ora [-u user] [-i instance#] &lt;command&gt; [] General -u user/pass use USER/PASS to log in -i instance# append # to ORACLE_SID -sid &lt;sid&gt; set ORACLE_SID to sid -top # limit some large queries ...

    cfssl_v1.61_arm64_6in1.zip

    cfssl   master ±  ll |grep cfssl -rwxr-xr-x 1 starwu staff 16578536 1 14 14:52 cfssl* -rwxr-xr-x 1 starwu staff 12350640 1 14 15:05 cfssl-bundle* -rwxr-xr-x 1 starwu staff 12526560 1 14 15:06 ...

    taily:尾巴罗兹logz。 现在有了颜色!

    安装在本地npm install taily 或全球npm install taily -g 用法taily [options]Options:-h, --help output usage information-V, --version output the version number-g, --grep [regex] Grep the results of the ...

    bash-exporter:用于bash脚本的简单,简约的Prometheus导出器,用Go编写

    hostname="node-1",job="job-2",verb="put"} 0.13bash{env="",hostname="node-1",job="job-2",verb="time"} 0.5bash{env="dev",hostname="",job="job-1",verb="items"} 21用法Usage of ./bash-exporter: -debug ...

    Android代码-httpclient-android

    Usage Download the repository and simply hit ./build.sh script Dependencies Required dependencies are: JDK 6 or newer Gradle 2.4 or newer find, grep, svn, patch and sed (or gsed for OS X) for ...

    2009 达内Unix学习笔记

    集合了 所有的 Unix命令大全 ...telnet 192.168.0.23 自己帐号 sd08077-you0 ftp工具 192.168.0.202 tools-toolss ... 各个 shell 可互相切换 ksh:$ sh:$ csh:guangzhou% bash:bash-3.00$ ... 命令和参数之间必需用空格隔...

    aGdaREP:在 Agda 中实现抓地力

    aGdaREP-在Agda中实施grep Usage: aGdaREP [OPTIONS] PATTERN [FILENAME] OPTIONS: -h Print this help -V Version -v Invert the match -i Ignore case 要求 该项目应使用以下命令进行编译: Agda 2.6.1.1版...

    vim插件打包

    "grep " "omnicppcomplete { set tags=tags; "智能补全ctags -R --c++-kinds=+p --fields=+iaS --extra=+q let OmniCpp_NamespaceSearch = 1 let OmniCpp_GlobalScopeSearch = 1 let OmniCpp_ShowAccess = 1 let...

    基于mediastreamer2的网络电话实现流程以及源码库

    于是:#grep "rpl_malloc" -nR * ....... 找到了,原来这两个变量是在这个config的文件中的。是一个宏开关 那么就好办了,我们就直接把这两个宏进行注释。 嗯嗯,继续。。。我们重新编译json库。。。嗯嗯编译好了,...

    (重要)AIX command 使用总结.txt

    End of File after: 252 Records, 526729216 Bytes. ---文件大小 tcopy: The end of the tape is reached. tcopy: The total tape length is 526729216 bytes. #如何取定文件与文件集的对应关系,有时想使用某个...

    Linux命令大全完整版

    statserial(status ofserial port) 166 talk 166 Tcpdump 167 testparm(test parameter) 168 traceroute 168 tty(teletypewriter) 169 uuname 169 wall(write all) 170 write 170 ytalk 170 arpwatch(ARP ...

    Advanced Bash-Scripting Guide <>

    Examples of sed operators D-1. "Reserved" Exit Codes L-1. Batch file keywords / variables / operators, and their shell equivalents L-2. DOS commands and their UNIX equivalents N-1. Revision History ...

    Linux高级bash编程

    Examples of sed operators D-1. "Reserved" Exit Codes L-1. Batch file keywords / variables / operators, and their shell equivalents L-2. DOS commands and their UNIX equivalents N-1. Revision History ...

    adb1.0.26包含fastboot.exe

    List of devices attached cf264b8f device emulator-5554 device 10.129.164.6:5555 device 输出里的 cf264b8f、emulator-5554 和 10.129.164.6:5555 即为 serialNumber。 比如这时想指定 cf264b8f 这个设备来运行...

Global site tag (gtag.js) - Google Analytics