linux shell 脚本攻略学习18--grep命令详解
grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是unix/linux中用于文本搜索的大师级的工具。它能够接受正则表达式和通配符。
首先,输入grep --help查看帮助信息:
amosli@amosli-pc:~$ grep --help
Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE or standard input.
PATTERN is, by default, a basic regular expression (BRE).
Example: grep -i 'hello world' menu.h main.c Regexp selection and interpretation:
-E, --extended-regexp PATTERN is an extended regular expression (ERE)
-F, --fixed-strings PATTERN is a set of newline-separated fixed strings
-G, --basic-regexp PATTERN is a basic regular expression (BRE)
-P, --perl-regexp PATTERN is a Perl regular expression
-e, --regexp=PATTERN use PATTERN for matching
-f, --file=FILE obtain PATTERN from FILE
-i, --ignore-case ignore case distinctions
-w, --word-regexp force PATTERN to match only whole words
-x, --line-regexp force PATTERN to match only whole lines
-z, --null-data a data line ends in byte, not newline Miscellaneous:
-s, --no-messages suppress error messages
-v, --invert-match select non-matching lines
-V, --version print version information and exit
--help display this help and exit
--mmap ignored for backwards compatibility Output control:
-m, --max-count=NUM stop after NUM matches
-b, --byte-offset print the byte offset with output lines
-n, --line-number print line number with output lines
--line-buffered flush output on every line
-H, --with-filename print the file name for each match
-h, --no-filename suppress the file name prefix on output
--label=LABEL use LABEL as the standard input file name prefix
-o, --only-matching show only the part of a line matching PATTERN
-q, --quiet, --silent suppress all normal output
--binary-files=TYPE assume that binary files are TYPE;
TYPE is `binary', `text', or `without-match'
-a, --text equivalent to --binary-files=text
-I equivalent to --binary-files=without-match
-d, --directories=ACTION how to handle directories;
ACTION is `read', `recurse', or `skip'
-D, --devices=ACTION how to handle devices, FIFOs and sockets;
ACTION is `read' or `skip'
-R, -r, --recursive equivalent to --directories=recurse
--include=FILE_PATTERN search only files that match FILE_PATTERN
--exclude=FILE_PATTERN skip files and directories matching FILE_PATTERN
--exclude-from=FILE skip files matching any file pattern from FILE
--exclude-dir=PATTERN directories that match PATTERN will be skipped.
-L, --files-without-match print only names of FILEs containing no match
-l, --files-with-matches print only names of FILEs containing matches
-c, --count print only a count of matching lines per FILE
-T, --initial-tab make tabs line up (if needed)
-Z, --null print byte after FILE name Context control:
-B, --before-context=NUM print NUM lines of leading context
-A, --after-context=NUM print NUM lines of trailing context
-C, --context=NUM print NUM lines of output context
-NUM same as --context=NUM
--color[=WHEN],
--colour[=WHEN] use markers to highlight the matching strings;
WHEN is `always', `never', or `auto'
-U, --binary do not strip CR characters at EOL (MSDOS)
-u, --unix-byte-offsets report offsets as if CRs were not there (MSDOS) `egrep' means `grep -E'. `fgrep' means `grep -F'.
Direct invocation as either `egrep' or `fgrep' is deprecated.
With no FILE, or when FILE is -, read standard input. If less than two FILEs
are given, assume -h. Exit status is if any line was selected, otherwise;
if any error occurs and -q was not given, the exit status is .
语法格式:
grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]
参数-实例:
test.txt
amosli@amosli-pc:~/learn/grep$ cat test.txt
hi,amos
this is linux world
grep command is powerful!
1.在文件中搜索一个单词
iamosli@amosli-pc:~/learn/grep$ grep 'hi' test.txt
hi,amos
this is linux world
2.从标准输入中搜索一个单词
amosli@amosli-pc:~/learn/grep$ echo this is a word | grep hi
this is a word
也可以加上参数-e,表示根据模式进行匹配,下面部分也将会对-e参数进行介绍
amosli@amosli-pc:~/learn/grep$ echo this is a word | grep -e hi
this is a word
3.-E参数,使用正则表达式进行搜索关键字
amosli@amosli-pc:~/learn/grep$ echo this is a word | grep -E [a-z]+d
this is a word
-E参数英文提示信息:
-E, --extended-regexp PATTERN is an extended regular expression (ERE)
意思是使用扩展(extended)的正则表达式.在文章开始的提示信息中有这么一段"egrep' means `grep -E'. ",所以也可以直接使用egrep命令进行搜索
amosli@amosli-pc:~/learn/grep$ echo this is a word | egrep "[a-z]+d"
this is a word
4.-o参数,只输出匹配到的文本部分,没有匹配到的不显示
amosli@amosli-pc:~/learn/grep$ echo this a line. | grep -o -E "[a-z]+\."
line.
#或者
amosli@amosli-pc:~/learn/grep$ echo this a line. | egrep -o "[a-z]+\."
line.
英文提示信息:
-o, --only-matching show only the part of a line matching PATTERN
5.--color参数,重点标记出匹配到的文本
amosli@amosli-pc:~/learn/grep$ echo this a line. | egrep -o "[a-z]+\." --color=auto
line.
除了auto之外,还可以选择never和always,这个是表示什么时候重点标记。
英文提示信息:
--colour[=WHEN] use markers to highlight the matching strings;
WHEN is `always', `never', or `auto'
6.-v参数,打印除了匹配到的文本以外的内容
常用格式:
grep -v match_pattern file
例:
amosli@amosli-pc:~/learn/grep$ grep -v 'hi,amos' test.txt
this is linux world
grep command is powerful!
英文提示信息:
-v, --invert-match select non-matching lines
7.-c参数,统计关键词出现的行数
amosli@amosli-pc:~/learn/grep$ grep -c hi test.txt
英文提示信息:
-c, --count print only a count of matching lines per FILE
这里grep只是统计出现的行数,而不是统计关键词出现的次数
如:
amosli@amosli-pc:~/learn/grep$ echo -e "1 2 3\nhelo\n 4 5" | grep -c "[0-9]"
这里有5个数字,但只有3行,其中有两行中出现数字,所以显示的次数为2
如何统计文件中关键词出现的次数?
可以使用wc 命令进行二次统计,如下:
amosli@amosli-pc:~/learn/grep$ echo -e "1 2 3\nhelo\n 4 5" | grep "[0-9]" | wc -w
8.-n参数,打印关键词所在行数
amosli@amosli-pc:~/learn/grep$ grep -n 'hi' test.txt
:hi,amos
:this is linux world
在第1行显示了关键词所在的行,极大的方便了阅读。
英文提示信息:
-n, --line-number print line number with output lines
--line-buffered flush output on every line
9.对多个文件进行搜索
test2.txt
amosli@amosli-pc:~/learn/grep$ cat test2.txt
hi,amos
this is linux world
Welcome you!
对test.txt 和test2.txt进行批量查询:
amosli@amosli-pc:~/learn/grep$ grep -n 'linux' test.txt test2.txt
test.txt::this is linux world
test2.txt::this is linux world
10.-l参数,对多个文件进行搜索,但只显示文件名
amosli@amosli-pc:~/learn/grep$ grep -l 'linux' test.txt test2.txt
test.txt
test2.txt
英文提示信息:
-l, --files-with-matches print only names of FILEs containing matches
11.-i 参数,忽略大小写进行搜索
amosli@amosli-pc:~/learn/grep$ echo "THIS IS test" | grep -i "th"
THIS IS test
英文提示信息:
-i, --ignore-case ignore case distinctions
12.-R参数,递归进行搜索
amosli@amosli-pc:~/learn/grep$ cd ..
amosli@amosli-pc:~/learn$ grep 'hi,amos' . -R
./grep/test.txt:hi,amos
./grep/test2.txt:hi,amos
英文提示信息:
-R, -r, --recursive equivalent to --directories=recurse
--include=FILE_PATTERN search only files that match FILE_PATTERN
--exclude=FILE_PATTERN skip files and directories matching FILE_PATTERN
--exclude-from=FILE skip files matching any file pattern from FILE
--exclude-dir=PATTERN directories that match PATTERN will be skipped.
13.-e参数,用grep匹配多个样式,即多个条件进行查询
amosli@amosli-pc:~/learn/grep$ grep -e "hi" -e "linux" -e "grep" test.txt
hi,amos
this is linux world
grep command is powerful!
英文提示信息:
-e, --regexp=PATTERN use PATTERN for matching
还有一种方法可以进行样式匹配,即把所要匹配的关键词用一个文件保存起来,然后进行检索,请看下一个参数-f
14.-f参数,用grep匹配多个样式,即多个条件进行查询--方法2
将关键词保存到f.txt中:
amosli@amosli-pc:~/learn/grep$ cat f.txt
hi
linux
grep
开始进行匹配:
amosli@amosli-pc:~/learn/grep$ grep -f f.txt test.txt
hi,amos
this is linux world
grep command is powerful!
结果跟-e参数一样,但在大文件多关键词的条件下,-f参数绝对提高了查询的效率。
英文提示信息:
-f, --file=FILE obtain PATTERN from FILE
15.在grep搜索中包括或排除指定的文件(--include参数和--exclude参数)
amosli@amosli-pc:~/learn/grep$ cat test.html
hi,amos
this is linux world
grep command is powerful!
开始检索只包括.html的文件:
amosli@amosli-pc:~/learn/grep$ grep "hi,amos" . -r --include *.html -n
./test.html::hi,amos
开始检索不包括.html的文件:
amosli@amosli-pc:~/learn/grep$ grep "hi,amos" . -r --exclude *.html -n
./test.txt::hi,amos
./test2.txt::hi,amos
英文提示信息:
-r, --recursive equivalent to --directories=recurse
--include=FILE_PATTERN search only files that match FILE_PATTERN
--exclude=FILE_PATTERN skip files and directories matching FILE_PATTERN
--exclude-from=FILE skip files matching any file pattern from FILE
--exclude-dir=PATTERN directories that match PATTERN will be skipped.
16.-A,-B,-C ,打印出关键词前、后的行
amosli@amosli-pc:~/learn/grep$ echo -e "1\n2\n3\n4\n5\n6\n7\n8\n9\n10" | grep ''
-A,打印出关键词之后的5行,after
amosli@amosli-pc:~/learn/grep$ echo -e "1\n2\n3\n4\n5\n6\n7\n8\n9\n10" | grep '' -A
-B,打印出关键词之前的2行,before
amosli@amosli-pc:~/learn/grep$ echo -e "1\n2\n3\n4\n5\n6\n7\n8\n9\n10" | grep '' -B
-C,打印出关键词前后2行
amosli@amosli-pc:~/learn/grep$ echo -e "1\n2\n3\n4\n5\n6\n7\n8\n9\n10" | grep '' -C
英文提示信息:
-B, --before-context=NUM print NUM lines of leading context
-A, --after-context=NUM print NUM lines of trailing context
-C, --context=NUM print NUM lines of output context
17.-q 参数,静默模式输出
在静默模式中,grep 命令不会向标准输出打印任何输出,它仅是运行命令,然后根据命令执行成功与否返回退出状态,没有找到值则返回状态为1,找到则返回0.
没有找到关键词:
amosli@amosli-pc:~/learn/grep$ grep -q 'abc' test.txt
amosli@amosli-pc:~/learn/grep$ $?
: command not found
找到关键词:
amosli@amosli-pc:~/learn/grep$ grep -q 'amos' test.txt
amosli@amosli-pc:~/learn/grep$ $?
: command not found
英文提示信息:
-q, --quiet, --silent suppress all normal output
--binary-files=TYPE assume that binary files are TYPE;
TYPE is `binary', `text', or `without-match'
18、其他参数,可输入man grep查看grep命令手册
linux shell 脚本攻略学习18--grep命令详解的更多相关文章
- linux shell 脚本攻略学习12--文件权限详解,chmod命令详解,chown命令详解,chattr命令详解
文件权限详解 一.chmod命令详解 文件权限和所有权是Unix/Linux文件系统最显著的特征之一.linux中的每一个文件都与多种权限类型相关联,在这些权限中主要分类为3种: 用户(User)是文 ...
- linux shell 脚本攻略学习6-xargs详解
xargs是一条Unix和类Unix操作系统的常用命令.它的作用是将参数列表转换成小块分段传递给其他命令,以避免参数列表过长的问题. 例如,下面的命令: rm `find /path -type f` ...
- linux shell 脚本攻略学习20--awk命令入门详解
awk生于1977年,创始人有三个,分别为 Alfred Aho,Peter Weinberger, 和 Brian Kernighan,名称源于三个创始人的姓的首字母. 作用:处理文本文件. awk ...
- linux shell 脚本攻略学习16--wc命令详解,tree命令详解
在文本处理的工作中,统计文件的行数,单词数和字符数非常有用.而对于开发人员本身来说,统计LOC(line of code ,代码行数)是一件重要的工作.linux中有什么命令可以帮助我们做统计呢?没错 ...
- linux shell 脚本攻略学习14--head命令详解,tail命令详解
当要查看上千行的大文件时,我们可不会用cat命令把整个文件内容给打印出来,相反,我们可能只需要看文件的一小部分地内容(例如文件的前十行和后十行),我们也有可能需要打印出来前n行或后n行,也有可能打印除 ...
- linux shell 脚本攻略学习13--file命令详解,diff命令详解
一.file命令详解 find命令可以通过查看文件内容来找出特定类型的文件,在UNIX/ Linux系统中,文件类型并不是由文件扩展名来决定的(windows中却正是这么做的),file命令的目的是从 ...
- linux shell 脚本攻略学习11--mkdir和touch命令详解
一.创建目录(mkdir命令详解) amosli@amosli-pc:~/learn$ mkdir dir amosli@amosli-pc:~/learn/dir$ mkdir folder amo ...
- linux shell 脚本攻略学习9--rename命令详解
rename命令详解: 对文件重命名是常用的操作之一,一般对单个文件的重命名用mv命令,如: amosli@amosli-pc:~/learn/example$ ls abc.txt amosli@a ...
- linux shell 脚本攻略学习8---md5校验,sort排序,uniq命令详解
一.校验与核实 目前最为出名的校验技术是md5sum和sha1sum,它们对文件内容使用相应的算法来生成校验和. 举例: amosli@amosli-pc:~/learn$ md5sum text.t ...
- linux shell 脚本攻略学习7---tr命令详解
tr命令详解 什么是tr命令?tr,translate的简写,translate的翻译: [trænsˈleit] vi. 翻译, 能被译出 vt. 翻译, 解释, 转化, 转变为, 调动 在这里用到 ...
随机推荐
- 用Visual C#来清空回收站(2)
四.程序的源代码(recycled.cs).编译方法及运行后的界面: (1).程序的源代码:recycled.cs: using System.IO ; using System.Windows.Fo ...
- 揭秘uc浏览器三
这节我们主要讨论收藏与历史记录页面的边边角角. 首先,看看他的最终的效果图了: 照例了,我们先看看他的布局文件: <!-- tab布局文件 --> <TabHost xmlns:an ...
- Mac系统下编译支持Android平台的最新X264编码器
Mac系统下编译支持Android平台的最新X264编码器 原文来自 http://www.mingjianhua.com,转载请注明出处 1.首先去官网下载最新的x264源代码,解压到任意目录 ht ...
- 条件随机场(CRF)原理和实现
版权声明:作者:金良山庄,欲联系请评论博客或私信,个人主页:http://www.jinliangxu.com/,CSDN博客: http://blog.csdn.net/u012176591 目 ...
- Sqlite的多表连接更新
最近处理一个较大数据的sqlite库,基础表300万条,结果表30万条左右,我的笔记本跑起来还算流畅.最后结果,需要两个表连接,把另一个表的计算结果更新过来,却遇到麻烦.sqliter并不支持常见的连 ...
- mongodb的用法
关于新版(2.***)的c#用法,网上基本没有.昨天折腾半天,去构造server,发现现在新版本不需要了,文档是这样的,大概意思,无需像原来那样获取server,直接从client获取db就行了. h ...
- Chrome中的哪些端口是限制使用的?
The following is a list of all of the restricted ports on Chrome: 1, // tcpmux 7, // echo 9, // di ...
- Asp.NET websocket,Asp.NET MVC 使用 SignalR 实现推送功能一(Hubs 在线聊天室)
ASP .NET SignalR 是一个ASP .NET 下的类库,可以在ASP .NET 的Web项目中实现实时通信.什么是实时通信的Web呢?就是让客户端(Web页面)和服务器端可以互相通知消息及 ...
- iOS开发点滴 - 关闭键盘
有时候系统显示的键盘会挡住视图中某些重要的控件,这个时候当用户按下换行键,就应该取消UITextField对象的第一响应(First Responder)状态而关闭键盘. 1. 首先,视图控制器必须遵 ...
- 微信小程序 - 骨架屏
骨架屏 - “与其等待网络加载,不如提前给点暗示” 注:不适用复杂交互效果 演示 示例解释以及使用全在index.wxml中,观看需了解组件使用. 示例下载:微信小程序-骨架屏演示