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命令详解的更多相关文章

  1. linux shell 脚本攻略学习12--文件权限详解,chmod命令详解,chown命令详解,chattr命令详解

    文件权限详解 一.chmod命令详解 文件权限和所有权是Unix/Linux文件系统最显著的特征之一.linux中的每一个文件都与多种权限类型相关联,在这些权限中主要分类为3种: 用户(User)是文 ...

  2. linux shell 脚本攻略学习6-xargs详解

    xargs是一条Unix和类Unix操作系统的常用命令.它的作用是将参数列表转换成小块分段传递给其他命令,以避免参数列表过长的问题. 例如,下面的命令: rm `find /path -type f` ...

  3. linux shell 脚本攻略学习20--awk命令入门详解

    awk生于1977年,创始人有三个,分别为 Alfred Aho,Peter Weinberger, 和 Brian Kernighan,名称源于三个创始人的姓的首字母. 作用:处理文本文件. awk ...

  4. linux shell 脚本攻略学习16--wc命令详解,tree命令详解

    在文本处理的工作中,统计文件的行数,单词数和字符数非常有用.而对于开发人员本身来说,统计LOC(line of code ,代码行数)是一件重要的工作.linux中有什么命令可以帮助我们做统计呢?没错 ...

  5. linux shell 脚本攻略学习14--head命令详解,tail命令详解

    当要查看上千行的大文件时,我们可不会用cat命令把整个文件内容给打印出来,相反,我们可能只需要看文件的一小部分地内容(例如文件的前十行和后十行),我们也有可能需要打印出来前n行或后n行,也有可能打印除 ...

  6. linux shell 脚本攻略学习13--file命令详解,diff命令详解

    一.file命令详解 find命令可以通过查看文件内容来找出特定类型的文件,在UNIX/ Linux系统中,文件类型并不是由文件扩展名来决定的(windows中却正是这么做的),file命令的目的是从 ...

  7. linux shell 脚本攻略学习11--mkdir和touch命令详解

    一.创建目录(mkdir命令详解) amosli@amosli-pc:~/learn$ mkdir dir amosli@amosli-pc:~/learn/dir$ mkdir folder amo ...

  8. linux shell 脚本攻略学习9--rename命令详解

    rename命令详解: 对文件重命名是常用的操作之一,一般对单个文件的重命名用mv命令,如: amosli@amosli-pc:~/learn/example$ ls abc.txt amosli@a ...

  9. linux shell 脚本攻略学习8---md5校验,sort排序,uniq命令详解

    一.校验与核实 目前最为出名的校验技术是md5sum和sha1sum,它们对文件内容使用相应的算法来生成校验和. 举例: amosli@amosli-pc:~/learn$ md5sum text.t ...

  10. linux shell 脚本攻略学习7---tr命令详解

    tr命令详解 什么是tr命令?tr,translate的简写,translate的翻译: [trænsˈleit] vi. 翻译, 能被译出 vt. 翻译, 解释, 转化, 转变为, 调动 在这里用到 ...

随机推荐

  1. JavaScript类的写法

    js类的基本含义 我们知道,在js中,是没有类的概念的.类的所有实例对象都从同一个原型对象上继承属性,因此,原型对象是类的核心. 类是对象的抽象,而对象是类的具体实例.类是抽象的,不占用内存,而对象是 ...

  2. Java Math.sqrt()方法

    描述 java.lang.Math.sqrt(double a) 返回正确舍入的一个double值的正平方根.特殊情况: 如果参数是NaN或小于为零,那么结果是NaN. 如果参数是正无穷大,那么结果为 ...

  3. [Math]理解卡尔曼滤波器 (Understanding Kalman Filter) zz

    1. 卡尔曼滤波器介绍 卡尔曼滤波器的介绍, 见 Wiki 这篇文章主要是翻译了 Understanding the Basis of the Kalman Filter Via a Simple a ...

  4. .NET-分页处理方式

    分页方案一: 现在常见的前端框架datatable,easyui等的分页插件,都是采用的前端分页,原理:先将符合条件的数据全部加载到页面上,然后计算分页,进行分页处理.(装载全部数据) 优点: --在 ...

  5. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(十九)ES6.2.2 安装Ik中文分词器

    注: elasticsearch 版本6.2.2 1)集群模式,则每个节点都需要安装ik分词,安装插件完毕后需要重启服务,创建mapping前如果有机器未安装分词,则可能该索引可能为RED,需要删除后 ...

  6. 转:android root tcpdump抓包强烈推荐

    转:http://www.cnblogs.com/findyou/p/3491035.html 写的相当详细且完整,业界良心. adb push d:\tcpdump /data/local/ adb ...

  7. MySQL数据库localhost的root用户登陆遭遇失败

    问题:Access denied for user 'root'@'localhost' (using password: YES)打开MySQL目录下的my.ini文件(Linux的话是/etc/m ...

  8. Coursera公开课笔记: 斯坦福大学机器学习第六课“逻辑回归(Logistic Regression)” 清晰讲解logistic-good!!!!!!

    原文:http://52opencourse.com/125/coursera%E5%85%AC%E5%BC%80%E8%AF%BE%E7%AC%94%E8%AE%B0-%E6%96%AF%E5%9D ...

  9. 整数对A满足二叉查找树,B满足最大堆

    1 题目 给出一组整数对 { (a[0], b[0]), (a[1], b[1]) ... (a[n-1], b[n-1]) },全部 a 值和 b 值分别不反复(随意 i != j 满足 a[i] ...

  10. mssql批量删除数据库里所有的表

    go declare @tbname varchar(250) declare #tb cursor for select name from sysobjects where objectprope ...