grep可以完成不必打开ascii文件就可以搜索其内容,其帮助如下:

$ 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           一个 0 字节的数据行,但不是空行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-recursivelikewise, but follow all symlinks--include=FILE_PATTERNsearch only files that match FILE_PATTERN--exclude=FILE_PATTERNskip 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 0 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 参数。
如果有任意行被匹配,那退出状态为 0,否则为 1;
如果有错误产生,且未指定 -q 参数,那退出状态为 2。请将错误报告给: bug-grep@gnu.org
GNU Grep 主页: <http://www.gnu.org/software/grep/>
GNU 软件的通用帮助: <http://www.gnu.org/gethelp/>

基本用法1:匹配字符串

$ grep "cdefg" a.txt b.txt

基本用法2:输出对应行号

$ grep "cdefg" a.txt -n

基本用法3:输出不匹配的行

$ grep "cdefg" a.txt b.txt -n -v

基本用法4:高光显示匹配的字符串

$ grep "cdefg" a.txt b.txt -n --color=auto

基本用法5:只输出匹配的部分

$ grep "cdefg" a.txt b.txt -n -o --color=auto

基本用法6:正则表达式

$ grep -E [a-c,2-4] b.txt c.txt -n --color=auto #或
$ egrep [a-c,2-4] b.txt c.txt -n --color=auto

采用不同的正则表达式:

$ egrep [1-2]+[2-4] b.txt c.txt -n --color=auto

$ egrep [1-2]+[2-4]+5 b.txt c.txt -n --color=auto

基本用法7:返回匹配的总行数

$ grep a b.txt c.txt -c

基本用法8:字节偏移

$ grep -b -o "ab" a.txt b.txt
a.txt:2:ab
a.txt:5:ab
a.txt:9:ab
a.txt:14:ab
a.txt:20:ab
a.txt:27:ab
a.txt:35:ab
a.txt:44:ab
a.txt:54:ab
a.txt:65:ab
a.txt:77:ab
a.txt:90:ab

基本用法9:返回字符串所在文件

$ grep -l "ab" a.txt b.txt c.txt
a.txt

基本用法10:不区分大小写

$ grep -i "aB" a.txt b.txt c.txt

基本用法11:管道

$ echo No Pain, No Gain | grep -i -e "nO" -e gA -n --color=auto

$ echo No Pain, No Gain | grep -i -e "nO" -e gA -n --color=auto | tee d.txt
$ ls
a.txt  b.txt  c.txt  d.txt
$ more d.txt
1:No Pain, No Gain

基本用法12:《递归》--匹配当前目录下所有文件

$ grep "abc" . -r

基本用法13:匹配多个字符(串)

$ grep -e "abc" -e "234" . -r

多个字符串匹配也可以从文件中设定字符串

$ echo abc > e.txt
$ grep -f e.txt . -r
./a.txt:abc
./a.txt:abcd
./a.txt:abcde
./a.txt:abcdef
./a.txt:abcdefg
./a.txt:abcdefgh
./a.txt:abcdefghi
./a.txt:abcdefghij
./a.txt:abcdefghijk
./a.txt:abcdefghijkl
./a.txt:abcdefghijklm
./e.txt:abc

基本用法14:搜索特定文件

$ grep -f e.txt . -r --include *.{txt,c} #搜索以txt和c结尾的文件
$ grep "abc" . -r --exclude *.{txt,c} #不搜索以txt和c结尾的文件
$ grep -f e.txt . -r --exclude-from d.txt #从文件中获取略过的文件名

基本用法15:条件语句

$  grep -q "abc" a.txt #找到匹配字符则返回1,否则返回0

基本用法15:显示匹配字符附近行

$ more c.txt
Just Do it.
Hello World !
My name is Linus.
GNU is not UNIX.
GNU/Linux
Just For Fun.
Hello.
Just Do It.
Don't push me so hard.
We don't talk anymore.
To Be No.1.
No pain, No Gain.
One Day.
$ grep -i hello c.txt -A 3 --color=auto #显示后三行
Hello World !
My name is Linus.
GNU is not UNIX.
GNU/Linux
--
Hello.
Just Do It.
Don't push me so hard.
We don't talk anymore.
$ grep -i hello c.txt -B 3 --color=auto #显示前三行
Just Do it.
Hello World !
--
GNU is not UNIX.
GNU/Linux
Just For Fun.
Hello.
$ grep -i hello c.txt -C 3 --color=auto #显示前后三行
Just Do it.
Hello World !
My name is Linus.
GNU is not UNIX.
GNU/Linux
Just For Fun.
Hello.
Just Do It.
Don't push me so hard.
We don't talk anymore.

Linux指令:grep指令详解1相关推荐

  1. Linux之grep命令详解

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

  2. linux中grep命令详解及使用示例

    文章目录 一.grep命令基本介绍 二.grep命令常见的用法 1. 匹配包含关键词的所有行 2. 匹配不包含关键词的所有行 3. 统计包含关键词的行数 4. 统计包含关键词所有行数,不区分大小写 5 ...

  3. 【Linux】grep正则表达式详解

    grep正则表达式 语法格式: grep [options] PATTERN [FILE...] grep [options] [-e PATTERN | -f FILE] [FILE...] 匹配模 ...

  4. linux xxx命令,linux命令ps aux|grep xxx详解

    对进程进行监测和控制,首先必须要了解当前进程的情况,也就是需要查看当前进程, 而ps命令(Process Status)就是最基本同时也是非常强大的进程查看命令. 使用该命令 可以确定有哪些进程正在运 ...

  5. linux中top工具,Linux命令工具 top详解

    Linux命令工具 top详解 top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.top是一个动态显示过程,即可以通过用户按键来不 ...

  6. Linux 中使用 sort 指令分组排序详解

    Linux 中使用 sort 指令分组排序详解 sort 中进行分组排序主要用到的选项为 -k,此文,我们着重于该选项的使用方式,用到的其它选项不做解释,有兴趣的同学可以查看帮助文档 1. 数据准备 ...

  7. hnc8进linux环境,最新发布linux指令大全总汇详解完整珍藏版-8

    最新发布linux指令大全总汇详解完整珍藏版-8.txt这是一个禁忌相继崩溃的时代,没人拦得着你,只有你自己拦着自己,你的禁忌越多成就就越少.自卑有多种档次,最高档次的自卑表现为吹嘘自己干什么都是天才 ...

  8. grep命令详解(如何匹配多个字符串)(grep指令)(grep -q)

    文章目录 20221011 grep如何匹配多个标志? 20230412 grep -q(`Use grep -q instead of comparing output with [ -n .. ] ...

  9. linux中top指令,Linux下top命令详解

    原标题:Linux下top命令详解 1.简介 top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.top显示系统当前的进程和其他状况 ...

  10. linux命令ps -aux|grep xxx详解

    linux命令ps -aux|grep xxx详解 要对进程进行监测和控制,首先必须要了解当前进程的情况,也就是需要查看当前进程, 而ps命令(Process Status)就是最基本同时也是非常强大 ...

最新文章

  1. windows下编译lua源码
  2. Ubuntu下mysql中文乱码的解决
  3. localdatetime 获取天_LocalDateTime的一些用法
  4. 【赛尔原创】用对比集成式方法理解基于文档的对话
  5. TurboMosaic 如何制作照片马赛克效果
  6. Dr.COM 宽带认证客户端频繁掉线问题解决方案
  7. USGS 官方批量下载软件bda 安装问题
  8. U盘插入,无法读取?6种解决方法
  9. java JVM 面试题----持续补充
  10. 关于如何调用苹果自带的地图APP
  11. 教你实现微信8.0『炸裂』的表情特效
  12. Word详细教程一(解决word护眼设置,但有些字底色仍是白色的)
  13. eNSP命令大全(所有命令)
  14. Oracle报错ORA-01507: ??????
  15. 第四届世界互联网大会人工智能论坛:AI让生活更美好
  16. java各历史版本官网下载
  17. 腾讯云自定义配置购买云服务器图文操作教程 新手必看!
  18. springBoot 在过滤器中如何捕获抛出的异常并自定义返回信息
  19. word2vec 中的数学原理详解(一)目录和前言
  20. Mac OS 输入adb命令无效(zsh: command not found: adb)的解决方法

热门文章

  1. 文献阅读005【精读】
  2. APP移动测试用例总结
  3. luogu_1984 [SDOI2008]烧水问题
  4. 用mac的chrome浏览器调试Android手机的网页
  5. 软件工程概论总结第四章
  6. 教你如何打开android4.3和4.4中隐藏的AppOps
  7. Solr相关概念详解:SolrRequestHandler
  8. AjaxControlToolkit工具控件之Accordion错误解决方法
  9. html 网站右侧导航,页面右侧固定导航.html
  10. 滚动模式_违停车辆有可能被强制拖车!嘉兴交警开启滚动式兵团化作战模式