我们一起玩转 Grep 指令

系统 Linux
grep这个linux指令大家一定不陌生,其用于查找文件中符合条件的字符串,下面来看看这个高频的指令如何使用。

在一个阳光明媚、晴空万里的中午,一个挠头的程序员正在与团队一姐排查超时问题,只见一姐手速极快的查找着一个又一个日志,快速定位到一个又一个嫌疑人,仰慕之情油然而生,为了后续也能够在小迷妹手上秀技术,所以暗下决心准备学习这个牛逼的东西。下面有请今天的主角(grep指令)闪亮登场。

一、基本语法

grep这个linux指令大家一定不陌生,其用于查找文件中符合条件的字符串,下面来看看这个高频的指令如何使用。

  1. grep [选项] 查找内容 [源文件] 

观察其组成结构,由四部分组成:指令名(grep)、选项、查找内容、源文件,其中需要注意的有两个位置,下面让我们徐徐道来。

源文件

源文件部分是可有可无的,若不指定任何文件名称或是所给予的文件名为-,则grep指令会从标准输入设备读取数据,其使用如下所示:

  1. // 文件路径为/test 
  2.  
  3. // 接收cat的输入 
  4. cat ./test |grep 'hello' 
  5.  
  6. // 存在路径部分参数 
  7. grep 'hello' ./test 

选项部分

选项部分比较多,可以通过grep --help指令来看一下有哪些选项:

  1. Regexp selection and interpretation: // 正则表达式选择和解释 
  2.   -E, --extended-regexp     PATTERN is an extended regular expression (ERE) 
  3.   -F, --fixed-strings       PATTERN is a set of newline-separated strings 
  4.   -G, --basic-regexp        PATTERN is a basic regular expression (BRE) 
  5.   -P, --perl-regexp         PATTERN is a Perl regular expression 
  6.   -e, --regexp=PATTERN      use PATTERN for matching 
  7.   -f, --file=FILE           obtain PATTERN from FILE 
  8.   -i, --ignore-case         ignore case distinctions 
  9.   -w, --word-regexp         force PATTERN to match only whole words 
  10.   -x, --line-regexp         force PATTERN to match only whole lines 
  11.   -z, --null-data           a data line ends in 0 byte, not newline 
  12.  
  13. Miscellaneous: // 各种各样的 
  14.   -s, --no-messages         suppress error messages 
  15.   -v, --invert-match        select non-matching lines // 搜索不匹配的行 
  16.   -V, --version             display version information and exit 
  17.       --help                display this help text and exit 
  18.  
  19. Output control: // 输出控制 
  20.   -m, --max-count=NUM       stop after NUM matches 
  21.   -b, --byte-offset         print the byte offset with output lines 
  22.   -n, --line-number         print line number with output lines 
  23.       --line-buffered       flush output on every line 
  24.   -H, --with-filename       print the file name for each match 
  25.   -h, --no-filename         suppress the file name prefix on output 
  26.       --label=LABEL         use LABEL as the standard input file name prefix 
  27.   -o, --only-matching       show only the part of a line matching PATTERN 
  28.   -q, --quiet, --silent     suppress all normal output 
  29.       --binary-files=TYPE   assume that binary files are TYPE; 
  30.                             TYPE is 'binary''text'or 'without-match' 
  31.   -a, --text                equivalent to --binary-files=text 
  32.   -I                        equivalent to --binary-files=without-match 
  33.   -d, --directories=ACTION  how to handle directories; 
  34.                             ACTION is 'read''recurse'or 'skip' 
  35.   -D, --devices=ACTION      how to handle devices, FIFOs and sockets; 
  36.                             ACTION is 'read' or 'skip' 
  37.   -r, --recursive           like --directories=recurse 
  38.   -R, --dereference-recursive  likewise, but follow all symlinks 
  39.       --include=FILE_PATTERN  search only files that match FILE_PATTERN 
  40.       --exclude=FILE_PATTERN  skip files and directories matching FILE_PATTERN 
  41.       --exclude-from=FILE   skip files matching any file pattern from FILE 
  42.       --exclude-dir=PATTERN  directories that match PATTERN will be skipped. 
  43.   -L, --files-without-match  print only names of FILEs containing no match 
  44.   -l, --files-with-matches  print only names of FILEs containing matches 
  45.   -c, --count               print only a count of matching lines per FILE 
  46.   -T, --initial-tab         make tabs line up (if needed) 
  47.   -Z, --null                print 0 byte after FILE name 
  48.  
  49. Context control: // 上下文控制 
  50.   -B, --before-context=NUM  print NUM lines of leading context 
  51.   -A, --after-context=NUM   print NUM lines of trailing context 
  52.   -C, --context=NUM         print NUM lines of output context 
  53.   -NUM                      same as --context=NUM 
  54.       --color[=WHEN], 
  55.       --colour[=WHEN]       use markers to highlight the matching strings; 
  56.                             WHEN is 'always''never'or 'auto' 
  57.   -U, --binary              do not strip CR characters at EOL (MSDOS/Windows) 
  58.   -u, --unix-byte-offsets   report offsets as if CRs were not there 

看着选项内容真的很多,背起来着实不易,幸好文档中给我们做了分类,只需要记住这些分类是干什么的,然后在需要的时候从里面进行搜索即可快速搜寻到所需用法(感觉看其内容必看菜鸟教程上的内容容易很多)

(1)当需要通过正则的方式进行搜索内容时,去"Regexp selection and interpretation"区块找选项即可,常用的有:

  1. -E:通过正则表达式进行搜索 

(2)当需要对输出的内容进行控制时,去"Output control"区块找选项即可,常用的有如下几个:

  1. -m 数量:表征匹配多少次就会停止 
  2. -n:显示匹配行及行号 
  3. -H:打印每一个匹配的文件名 
  4. -r:能够递归查询,即可以输入文件夹查询 
  5. -c:统计匹配到行的个数 

(3)当需要获取输出内容的上下文进行操纵时,去"Context control"区块找选项即可,常用的有如下几个:

  1. -B 数量、-A 数量、-C 数量:分别表征获取内容前、后、前后几行 
  2. --color:对输出的内容添加颜色 

(4)除了一些划分比较理解的选项,还有一些选项我个人认为划分的并不是很合理,但是它们仍然很重要,让我们一起来看看有哪些:

  1. -i:忽略字母大小写 
  2. -v:反向选择,也就是显示出没有搜索出字符串内容的那一行 

二、经典用法

上面已经将其基本使用做了详细的阐述,俗话说的好:光说不练假把式,光练不说真把式,连说带练全把式。既然上面阐述了一通理论的东西,下面我们就来实战几个常用场景,将理论付诸于实践。在实战之前先创建一个文件,文件名是test,文件内容如下所示:

  1. hello world!!! 
  2. dog 
  3. cat 
  4. pig 
  5. big pig 
  6. tiger 
  7. Elephant 

从确定文件中过滤出包含pig的

  1. $ grep 'pig' ./test 
  2. pig 
  3. big pig 

从包含某一部分内容的文件中过滤包含pig的

  1. $ grep 'pig' ./te* 
  2. pig 
  3. big pig 

从某一文件夹下所有内容中过滤出包含pig的

  1. $ grep -r 'pig' . 
  2. ./test:pig 
  3. ./test:big pig 

从某一文件中过滤出不包含pig的

  1. $ grep -v 'pig' ./test 
  2. hello world!!! 
  3. dog 
  4. cat 
  5. tiger 
  6. Elephant 

在过滤文件时显示行数

  1. $ grep -n 'pig' ./test 
  2. 4:pig 
  3. 5:big pig 

匹配出以开头的内容(通过基本正则表达式匹配即可,基本正则表达式字符有^$.[]*)

  1. $ grep ^p ./test 
  2. pig 

匹配出包含pig或cat内容的行(用到了扩展正则表达式,其在基本正则表达式基础上增加了(){}?+|等)

  1. $ grep -E 'pig|cat' ./test 
  2. cat 
  3. pig 
  4. big pig 

匹配出包含hello和world内容的行

  1. $ grep 'hello' ./test |grep 'world' 
  2. hello world!!! 

获取到匹配内容‘big pig'的前一行内容

  1. $ grep -B1 'big pig' ./test 
  2. pig 
  3. big pig 

获取匹配到'pig'行的数量

  1. $ grep -c 'pig' ./test 
  2.  

获取到的pig行的内容高亮显示

  1. $ grep --color 'pig' ./test 
  2. pig 
  3. big pig 

经典用法还有很多,不能再一一进行罗列了,只需要知道在过滤内容时用此技巧能解决80%的问题,但这就足够让自己成为最亮的那个崽。

·大家好,我是执鸢者,毕业于华中科技大学,新时代农民工,现在是百度前端研发工程师,著有《前端百题斩》、数十篇学习思维导图(go、React、Redux、Vue、Vuex、操作系统、Linux、设计模式、js、webpack、nginx)以及大量前端进阶文章。

本文转载自微信公众号「前端点线面」,可以通过以下二维码关注。转载本文请联系前端点线面公众号。

 

责任编辑:武晓燕 来源: 前端点线面
相关推荐

2021-12-29 08:27:05

ByteBuffer磁盘服务器

2022-09-26 14:25:55

Flowable流程ID

2022-02-22 10:50:19

IDEAGit工具,

2021-03-10 12:43:06

LDR指令函数

2021-08-27 07:06:10

IOJava抽象

2024-02-20 21:34:16

循环GolangGo

2021-07-28 07:53:20

Github ActiDotnet 应用

2022-03-08 17:52:58

TCP格式IP

2022-10-18 07:33:57

Maven构建工具

2021-08-27 07:06:09

DubboDocker技术

2022-03-31 18:59:43

数据库InnoDBMySQL

2023-08-10 08:28:46

网络编程通信

2023-08-04 08:20:56

DockerfileDocker工具

2023-09-10 21:42:31

2023-06-30 08:18:51

敏捷开发模式

2022-05-24 08:21:16

数据安全API

2021-01-12 05:08:49

DHCP协议模型

2016-09-06 10:39:30

Dell Techno

2022-11-12 12:33:38

CSS预处理器Sass

2022-04-06 08:23:57

指针函数代码
点赞
收藏

51CTO技术栈公众号