grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。

      (1)用法:

      用法:  grep [选项]... PATTERN [FILE]...

      (2)功能:

      功能:  在每个 FILE 或是标准输入中查找 PATTERN。

      (3)选项参数:

1) -V, --version           显示版本号

2) -i                在匹配过程中忽略大小写

3) -v, --invert-match         显示不匹配的行

4) -f                指定文件中存的每行字符串作为匹配字符串

5) -c                统计每个文件中包含指定字符串的行数

6) --color=auto            标记匹配颜色

7) -E                  将范本样式为延伸的普通表示法来使用,意味着使用能使用扩展正则表达式

8) -q                 grep静默输出,常用来测试。

      (4)实例:

1)[root@localhost grepDir]# grep "MenAngel" t1.txt t2.txt t3.txt          在特定的文本集中查找特定字符串

[root@localhost grepDir]# cat >t1.txt
I'm MenAngel!
Although I'm still a poor student right now,I believe that someday I will be one of the successful man in the world!
^Z
[]+ 已停止 cat > t1.txt
[root@localhost grepDir]# cat >t2.txt
Every one fights for a better future,but I fight for freedom!
^Z
[]+ 已停止 cat > t2.txt
[root@localhost grepDir]# cat >t3.txt <<EOF
> There is no one hoping that you will succeed when you are been looking down upon,but if you succeeded,they will look down upon themselves!
> When you get an important thing,you will find that it is not so precious as you like,but when you lose it after that,it will become so precious as you liked.
> EOF
[root@localhost grepDir]# grep "MenAngel" t1.txt t2.txt t3.txt
t1.txt:I'm MenAngel!

2)[root@localhost grepDir]# grep -v "MenAngel" t1.txt t2.txt t3.txt        输出指定字符串所在行之外的所有文件的行内容

[root@localhost grepDir]# grep -v "MenAngel" t1.txt t2.txt t3.txt
t1.txt:Although I'm still a poor student right now,I believe that someday I will be one of the successful man in the world!
t2.txt:Every one fights for a better future,but I fight for freedom!
t3.txt:There is no one hoping that you will succeed when you are been looking down upon,but if you succeeded,they will look down upon themselves!
t3.txt:When you get an important thing,you will find that it is not so precious as you like,but when you lose it after that,it will become so precious as you liked.

3)[root@localhost grepDir]# grep "fight" t1.txt t2.txt t3.txt --color=auto      将查找的字符串用特定的颜色标记出来

[root@localhost grepDir]# grep "fight" t1.txt t2.txt t3.txt --color=auto
t2.txt:Every one fights for a better future,but I fight for freedom!

4)[root@localhost grepDir]# grep -c "that" t1.txt t2.txt t3.txt            统计每个文件中包含指定字符串的行数

[root@localhost grepDir]# grep -c "that" t1.txt t2.txt t3.txt
t1.txt:
t2.txt:
t3.txt:

5)[root@localhost grepDir]# grep -n "that" t1.txt t2.txt t3.txt             默认情况,输出包含特定字符串所在的行

[root@localhost grepDir]# grep -n "that" t1.txt t2.txt t3.txt
t1.txt::Although I'm still a poor student right now,I believe that someday I will be one of the successful man in the world!
t3.txt::There is no one hoping that you will succeed when you are been looking down upon,but if you succeeded,they will look down upon themselves!
t3.txt::When you get an important thing,you will find that it is not so precious as you like,but when you lose it after that,it will become so precious as you liked.
[root@localhost grepDir]# grep "that" t1.txt t2.txt t3.txt
t1.txt:Although I'm still a poor student right now,I believe that someday I will be one of the successful man in the world!
t3.txt:There is no one hoping that you will succeed when you are been looking down upon,but if you succeeded,they will look down upon themselves!
t3.txt:When you get an important thing,you will find that it is not so precious as you like,but when you lose it after that,it will become so precious as you liked.

6)[root@localhost grepDir]# cat t1.txt t2.txt t3.txt|grep "MenAngel"        配合cat命令查看文件中指定字符串所在行的内容

[root@localhost grepDir]# cat t1.txt t2.txt t3.txt|grep "MenAngel"
I'm MenAngel!
[root@localhost grepDir]# grep "MenAngel" t1.txt t2.txt t3.txt
t1.txt:I'm MenAngel!

7)[root@localhost grepDir]# cat t1.txt t2.txt t3.txt|grep ^I             查找以指定字符串开头的文本行并输出

[root@localhost grepDir]# cat t1.txt t2.txt t3.txt|grep ^I
I'm MenAngel!
[root@localhost grepDir]# cat t1.txt t2.txt t3.txt|grep ^M //没有以M开头的
[root@localhost grepDir]#

8)[root@localhost grepDir]# cat t1.txt t2.txt t3.txt|grep ^[^I]            经不以指定字符串开头的文本所在行的内容输出

[root@localhost grepDir]# cat t1.txt t2.txt t3.txt|grep ^[^I]
Although I'm still a poor student right now,I believe that someday I will be one of the successful man in the world!
Every one fights for a better future,but I fight for freedom!
There is no one hoping that you will succeed when you are been looking down upon,but if you succeeded,they will look down upon themselves!
When you get an important thing,you will find that it is not so precious as you like,but when you lose it after that,it will become so precious as you liked.
[root@localhost grepDir]#

9)[root@localhost grepDir]# seq 10|grep "5" -C|-A|-B 3                 显示特定行的前面或后面的内容     

[root@localhost grepDir]# seq |grep "" -C 

[root@localhost grepDir]# seq |grep "" -A 

[root@localhost grepDir]# seq |grep "" -B 

10)[root@localhost grepDir]# grep -C 3 "MenAngel" t1.txt t2.txt t3.txt      无论用不用通道,参数都是可用的

[root@localhost grepDir]# grep -C  "MenAngel" t1.txt t2.txt t3.txt
t1.txt:I'm MenAngel!
t1.txt-Although I'm still a poor student right now,I believe that someday I will be one of the successful man in the world!

11)[root@localhost grepDir]# echo -e "a\nb\nc\na\nb\nc" | grep a -A 1      如果匹配结果有多个,会用“--”作为各匹配结果之间的分隔符

[root@localhost grepDir]# echo -e "a\nb\nc\na\nb\nc" | grep a -A
a
b
--
a
b
[root@localhost grepDir]#
[root@localhost grepDir]# echo -e "a\nb\nc\nd\na\nb\nc\nd" | grep a -A
a
b
--
a
b
[root@localhost grepDir]# echo -e "a\nb\nc\nd\na\nb\nc\nd" | grep a -A
a
b
c
--
a
b
c

12)[root@localhost grepDir]# echo this is a text line | grep -e "is" -e "line" -o          制动多个匹配样式

[root@localhost grepDir]# echo this is a text line | grep -e "is" -e "line" -o   //grep这里处理的是前面的echo输出的内容
is
is
line

13)[root@localhost grepDir]# echo MenAngel is sunjimeng|grep -f patfile            指定在文件中每行存的多个字符串

[root@localhost grepDir]# cat >patfile <<EOF
> MenAngel
> sunjimeng
> EOF
[root@localhost grepDir]# echo MenAngel is sunjimeng|grep -f patfile
MenAngel is sunjimeng
[root@localhost grepDir]#

14)[root@localhost grepDir]# grep --help

[root@localhost grepDir]# grep --help
用法: grep [选项]... PATTERN [FILE]...
在每个 FILE 或是标准输入中查找 PATTERN。
默认的 PATTERN 是一个基本正则表达式(缩写为 BRE)。
例如: grep -i 'hello world' menu.h main.c 正则表达式选择与解释:
-E, --extended-regexp PATTERN 是一个可扩展的正则表达式(缩写为 ERE)
-F, --fixed-strings PATTERN 是一组由断行符分隔的定长字符串。
-G, --basic-regexp PATTERN 是一个基本正则表达式(缩写为 BRE)
-P, --perl-regexp PATTERN 是一个 Perl 正则表达式
-e, --regexp=PATTERN 用 PATTERN 来进行匹配操作
-f, --file=FILE 从 FILE 中取得 PATTERN
-i, --ignore-case 忽略大小写
-w, --word-regexp 强制 PATTERN 仅完全匹配字词
-x, --line-regexp 强制 PATTERN 仅完全匹配一行
-z, --null-data 一个 字节的数据行,但不是空行 Miscellaneous:
-s, --no-messages suppress error messages
-v, --invert-match select non-matching lines
-V, --version display version information and exit
--help display this help text and exit 输出控制:
-m, --max-count=NUM NUM 次匹配后停止
-b, --byte-offset 输出的同时打印字节偏移
-n, --line-number 输出的同时打印行号
--line-buffered 每行输出清空
-H, --with-filename 为每一匹配项打印文件名
-h, --no-filename 输出时不显示文件名前缀
--label=LABEL 将LABEL 作为标准输入文件名前缀
-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, --recursive like --directories=recurse
-R, --dereference-recursive
likewise, but follow all symlinks
--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 文件控制:
-B, --before-context=NUM 打印以文本起始的NUM 行
-A, --after-context=NUM 打印以文本结尾的NUM 行
-C, --context=NUM 打印输出文本NUM 行
-NUM same as --context=NUM
--group-separator=SEP use SEP as a group separator
--no-group-separator use empty string as a group separator
--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/Windows)
-u, --unix-byte-offsets report offsets as if CRs were not there
(MSDOS/Windows) ‘egrep’即‘grep -E’。‘fgrep’即‘grep -F’。
直接使用‘egrep’或是‘fgrep’均已不可行了。
若FILE 为 -,将读取标准输入。不带FILE,读取当前目录,除非命令行中指定了-r 选项。
如果少于两个FILE 参数,就要默认使用-h 参数。
如果有任意行被匹配,那退出状态为 ,否则为 ;
如果有错误产生,且未指定 -q 参数,那退出状态为 。 请将错误报告给: bug-grep@gnu.org
GNU Grep 主页: <http://www.gnu.org/software/grep/>
GNU 软件的通用帮助: <http://www.gnu.org/gethelp/>

       grep的用法远远不止这么多,以后如果见得多了,用的多了,再更新!

每天一个Linux命令(34)grep命令的更多相关文章

  1. 每天一个linux命令(51)--grep命令

    linux系统中grep 命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来.grep 全称是 global regular expression print,表示全局正则 ...

  2. 【Linux命令】grep命令

    1.作用 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全 ...

  3. linux常用命令:grep 命令

    Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...

  4. Linux中利用grep命令如何检索文件内容详解

    前言 Linux系统中搜索.查找文件中的内容,一般最常用的是grep命令,另外还有egrep命令,同时vi命令也支持文件内容检索.下面来一起看看Linux利用grep命令检索文件内容的详细介绍. 方法 ...

  5. 每天一个linux命令(34)--top命令

    今天给领导发邮件,我这边虽然显示发出去了,但是他那边一直没收到,结果我以为我发了,他又一直在那边等结果.所以说,以后要另外发个信息或者QQ微信之类的说一声. top命令是Linux 下常用的性能分析工 ...

  6. Unix/Linux中的grep命令(转)

    本文转载自:如何使用Unix/Linux grep命令——磨刀不误砍柴工系列.该博文条理很清晰. grep简介 grep在一个或多个文件中查找与模式字符串(pattern)匹配的行,并将搜索的结果打印 ...

  7. Linux find、grep命令详细用法

    在linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find.grep命令,他哥俩可以算是必会的linux命令,我几乎每天都要用到他们.本文结构如下:find命令 find命令的一般形式 ...

  8. Linux中通过grep命令检索文件内容和指定内容前后几行

    原文链接: https://www.linuxidc.com/Linux/2017-11/148390.htm Linux系统中搜索.查找文件中的内容,一般最常用的是grep命令,另外还有egrep命 ...

  9. linux(centos8):用grep命令查找文件内容

    一,grep的用途: linux平台有最常用的三大文本处理工具:awk/sed/grep grep的功能:搜索指定文件的内容,按照指定的模式匹配,并输出匹配内容所在的行. 需要注意的地方:grep只支 ...

  10. 【Linux基础】grep命令

    1.简介 grep是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来. 命令格式:grep [option] pattern file 2.常用参数与举例: -e :  使用P ...

随机推荐

  1. spring aop中的propagation(传播属性)的7种配置的意思

      1.前言. 在声明式的事务处理中,要配置一个切面,即一组方法,如 <tx:advice id="txAdvice" transaction-manager="t ...

  2. Github上的PHP开源资源汇总

    依赖管理 ——用于依赖管理的包和框架 Composer/Packagist : 一个包和依赖管理器 Composer Installers:  一个多框架Composer库安装器 Pickle: 可以 ...

  3. background API

    语法: background:bg-color bg-image position/bg-size bg-repeat bg-origin bg-clip bg-attachment initial| ...

  4. Linux系统防CC攻击自动拉黑IP增强版Shell脚本 《Linux系统防CC攻击自动拉黑IP增强版Shell脚本》来自张戈博客

    前天没事写了一个防CC攻击的Shell脚本,没想到这么快就要用上了,原因是因为360网站卫士的缓存黑名单突然无法过滤后台,导致WordPress无法登录!虽然,可以通过修改本地hosts文件来解决这个 ...

  5. Maven常见异常及解决方法(本篇停更至16-4-12)

    本篇文章记录了老猫在学习整合Maven和SSH过程中遇到的问题,有的问题可以解决.有的问题还不能解决. 方法不一定适合全部的环境.但绝对是本人常遇到的常见异常.在这里做一个笔记和记录,也分享给大家,希 ...

  6. android开发游记:meterial design 5.0 开源控件整套合集 及使用demo

    android 的5.0公布不光google官方给出了一些新控件,同一时候还给出了一套符合material design风格的设计标准,这套标准将未来将覆盖google全部产品包括pc端,站点,移动端 ...

  7. ApplicationContextRunner如何简化自动配置测试

    1. 概览 众所周知,自动配置是Spring Boot的关键功能之一, 但测试自动配置可能会很棘手. 在以下部分中,我们将展示ApplicationContextRunner如何简化自动配置测试. 2 ...

  8. EasyNetQ操作RabbitMQ(高级消息队列)

    RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件).写消息队列的时候用RabbitMQ比较好,但是写的时候需要自己封装下,自己的封装,就需要对RabbitM ...

  9. iOS lipo 移除i386 x86_64两个平台

    由于 iOS 编译的特殊性,为了方便开发者使用,我们将 i386 x86_64 armv7 arm64 几个平台都合并到了一起, 所以使用动态库上传appstore时需要将i386 x86_64两个平 ...

  10. 系统服务-----NotificationManager

    熟悉api事例笔记: package com.test; import com.example.test.R; import android.app.Activity; import android. ...