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. 翻译, 解释, 转化, 转变为, 调动 在这里用到 ...
随机推荐
- SQL Server 损坏修复 之一 常见错误解读
SQL Server 对数据库损坏的错误类型做了细化,在此对几个典型的错误作一下介绍. 错误信息是:“在文件 '%ls'中.偏移量为 %#016I64x 的位置执行 %S_MSG 期间,操作系统已经向 ...
- java.sql.SQLException: ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值
问题来源:我在执行sql生成json并存入数据库是报的错. 原因:存json的字段我定义其类型为varchar2. 分析:这个异常是指,用户向数据库执行插入数据操作时,某条数据的某个字段值过长,如果是 ...
- 大数据开发实战:Hadoop数据仓库开发实战
1.Hadoop数据仓库架构设计 如上图. ODS(Operation Data Store)层:ODS层通常也被称为准备区(Staging area),它们是后续数据仓库层(即基于Kimball维度 ...
- spark0.8.0安装与学习
spark0.8.0安装与学习 原文地址:http://www.yanjiuyanjiu.com/blog/20131017/ 环境:CentOS 6.4, Hadoop 1.1.2, J ...
- Java-JUC(八):使用wait,notify|notifyAll完成生产者消费者通信,虚假唤醒(Spurious Wakeups)问题出现场景,及问题解决方案。
模拟通过线程实现消费者和订阅者模式: 首先,定义一个店员:店员包含进货.卖货方法:其次,定义一个生产者,生产者负责给店员生产产品:再者,定义一个消费者,消费者负责从店员那里消费产品. 店员: /** ...
- GIT 使用cherry-pick 重演其他分支的提交
在使用Git时是否会遇到这样的问题: 你正在使用Git进行版本控制,某天你接着昨天的工作了提交了N个提交,结果在合并远程分支的时候才发现原来你在工作之前没有注意到你要提交的分支状态 结果导致你本来要提 ...
- maven 打包可执行jar的方法
转自:http://blog.csdn.net/johnnywww/article/details/7964326 1.修改pom.xml增加如下内容 <plugin> <group ...
- java enum 用法
/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2010, Red Hat Inc. or th ...
- Centos下安装gcc
虚拟机安装了一个CentOS7,发现没有gcc,通过以下命令安装: yum install gcc yum install gcc-c++
- 关于LINUX在中断(硬软)中不能睡眠的真正原因
摘自http://bbs.chinaunix.net/thread-2115820-1-1.html 4楼的回答 先把中断处理流程给出来 1.进入中断处理程序--->2.保存关键上下文----& ...