N天学习一个Linux命令之grep
前言
任何系统都会出问题,出了问题一般怎么排查BUG?这个时候程序中记录的异常日志以及关键节点的日志就非常重要了,面对一大堆的日志文件,怎么找出我们需要的有用信息呢?linux中可以使用grep命令查找,这个命令的功能非常强大,也是我平时中排查线上错误时使用最多的命令之一。
命令名称
grep
用途
查找指定文件内包含指定关键字(正则表达式)的内容,按行为单位匹配
使用格式
grep [OPTIONS] PATTERN [FILE...]
常用选项
-V (显示命令版本)
正则模式匹配版本
-E, --extended-regexp (Interpret PATTERN as an extended regular expression)
-F, --fixed-strings (Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.)
-G, --basic-regexp (Interpret PATTERN as a basic regular expression (BRE, see below). This is the default.)
-P, --perl-regexp (Interpret PATTERN as a Perl regular expression.)
匹配模式控制
-e PATTERN (Use PATTERN as the pattern. This can be used to specify multiple search patterns.)
-f FILE, --file=FILE (Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing.)
-i, --ignore-case (Ignore case distinctions in both the PATTERN and the input files.)
-v, --invert-match (Invert the sense of matching, to select non-matching lines.)
-x, --line-regexp (整行匹配)
内容输出控制
-c, --count (只显示符合条件的总数量)
--color[=WHEN]
(Surround the matched (non-empty) strings, matching lines, context lines, file names, line numbers, byte offsets, and separators (for fields and groups of context lines)
with escape sequences to display them in color on the terminal. The colors are defined by the environment variable GREP_COLORS. WHEN is never, always, or auto.)
-L, --files-without-match (只输出未匹配的文件名列表 print the name of each input file from which no output would normally have been printed)
-l, --files-with-matches (只输出有匹配的文件名列表)
-m NUM, --max-count=NUM (一个文件只输出符合条件的行数<=NUM)
-o, --only-matching (只输出匹配的部分)
-q, --quiet, --silent (不输出任何内容)
-s, --no-messages (Suppress error messages about nonexistent or unreadable files.)
前缀内容输出控制
-b, --byte-offset
(Print the 0-based byte offset within the input file before each line of output. If -o (--only-matching) is specified, print the offset of the matching part itself.)
-H, --with-filename (输出文件名)
-h, --no-filename (不输出文件名)
--label=LABEL
(Display input actually coming from standard input as input coming from file LABEL. This is especially useful when implementing tools like zgrep, e.g., gzip -cd foo.gz |
grep --label=foo -H something. See also the -H option.)
-n, --line-number (显示行号)
-T, --initial-tab (行中每部分内容以tab结尾,比如:文件名tab行号tab内容)
-u, --unix-byte-offsets
(Report Unix-style byte offsets. This switch causes grep to report byte offsets as if the file were a Unix-style text file, i.e., with CR characters stripped off. This
will produce results identical to running grep on a Unix machine. This option has no effect unless -b option is also used; it has no effect on platforms other than MS-DOS
and MS-Windows.)
-Z, --null
(Output a zero byte (the ASCII NUL character) instead of the character that normally follows a file name. For example, grep -lZ outputs a zero byte after each file name
instead of the usual newline. This option makes the output unambiguous, even in the presence of file names containing unusual characters like newlines. This option can be
used with commands like find -print0, perl -0, sort -z, and xargs -0 to process arbitrary file names, even those that contain newline characters.)
上下文行输出控制
-A NUM, --after-context=NUM
(Print NUM lines of trailing context after matching lines. Places a line containing a group separator (described under --group-separator) between contiguous groups of
matches. With the -o or --only-matching option, this has no effect and a warning is given.)
-B NUM, --before-context=NUM
(Print NUM lines of leading context before matching lines. Places a line containing a group separator (described under --group-separator) between contiguous groups of
matches. With the -o or --only-matching option, this has no effect and a warning is given.)
-C NUM, -NUM, --context=NUM
(Print NUM lines of output context. Places a line containing a group separator (described under --group-separator) between contiguous groups of matches. With the -o or
--only-matching option, this has no effect and a warning is given.)
--group-separator=SEP (Use SEP as a group separator. By default SEP is double hyphen (--).)
--no-group-separator (Use empty string as a group separator.)
文件和目录设置
-a, --text (所有的文件中查找 this is equivalent to the --binary-files=text option.)
--binary-files=TYPE
(If the first few bytes of a file indicate that the file contains binary data, assume that the file is of type TYPE. By default, TYPE is binary, and grep normally outputs
either a one-line message saying that a binary file matches, or no message if there is no match. If TYPE is without-match, grep assumes that a binary file does not match;
this is equivalent to the -I option. If TYPE is text, grep processes a binary file as if it were text; this is equivalent to the -a option. Warning: grep
--binary-files=text might output binary garbage, which can have nasty side effects if the output is a terminal and if the terminal driver interprets some of it as commands.)
-D ACTION, --devices=ACTION
(If an input file is a device, FIFO or socket, use ACTION to process it. By default, ACTION is read, which means that devices are read just as if they were ordinary files.
If ACTION is skip, devices are silently skipped.)
-d ACTION, --directories=ACTION
(If an input file is a directory, use ACTION to process it. By default, ACTION is read, i.e., read directories just as if they were ordinary files. If ACTION is skip,
silently skip directories. If ACTION is recurse, read all files under each directory, recursively, following symbolic links only if they are on the command line. This is
equivalent to the -r option.)
--exclude=GLOB
(Skip files whose base name matches GLOB (using wildcard matching). A file-name glob can use *, ?, and [...] as wildcards, and \ to quote a wildcard or backslash character
literally.)
--exclude-from=FILE
(Skip files whose base name matches any of the file-name globs read from FILE (using wildcard matching as described under --exclude).)
--exclude-dir=DIR (Exclude directories matching the pattern DIR from recursive searches.)
-I (Process a binary file as if it did not contain matching data; this is equivalent to the --binary-files=without-match option.)
--include=GLOB (Search only files whose base name matches GLOB (using wildcard matching as described under --exclude).)
-r, --recursive
(Read all files under each directory, recursively, following symbolic links only if they are on the command line. This is equivalent to the -d recurse option.)
-R, --dereference-recursive (Read all files under each directory, recursively. Follow all symbolic links, unlike -r.)
其它选项
--line-buffered (Use line buffering on output. This can cause a performance penalty.)
-U, --binary
(Treat the file(s) as binary. By default, under MS-DOS and MS-Windows, grep guesses the file type by looking at the contents of the first 32KB read from the file. If grep
decides the file is a text file, it strips the CR characters from the original file contents (to make regular expressions with ^ and $ work correctly). Specifying -U
overrules this guesswork, causing all files to be read and passed to the matching mechanism verbatim; if the file is a text file with CR/LF pairs at the end of each line,
this will cause some regular expressions to fail. This option has no effect on platforms other than MS-DOS and MS-Windows.)
-z, --null-data
(Treat the input as a set of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. Like the -Z or --null option, this option can be used
with commands like sort -z to process arbitrary file names.)
实践
1.查找某个目录下所有包含keyword1或者keyword2的内容
grep -r -e 'keyword1' -e 'keyword2' $dirname
2.查找包含keyword1且文件名后缀带.log的内容
grep 'keyword1' *.log
3.查找包含keyword1的内容显示带行号
grep -n 'keyword1' $file
4.查找包含keyword1输出内容不显示文件名
grep -h 'keyword1' ./*
后记
有些配置项不是很理解,可能还没找到使用的场景吧,碰到使用的场景时,再好好琢磨琢磨 >_<.
参考资料
【1】man grep
N天学习一个Linux命令之grep的更多相关文章
- N天学习一个Linux命令之帮助命令:man
前言 工作中每天都在使用常用的命令和非常用的命令,忘记了用法或者参数,都会bing一下,然后如此循环.一直没有真正的系统的深入的去了解命令的用法,我决定打破它.以前看到有人,每天学习一个linux命令 ...
- N天学习一个linux命令之kill
用途 用于终止进程 用法 kill [-s signal|-p] [--] pid... kill -l [signal] 说明 1.默认发送信号15(请求终止进程,程序可以捕获,操作系统会杀死没有对 ...
- N天学习一个Linux命令之free
用途 查看系统内存(物理/虚拟/缓存/共享)使用情况 用法 free [-b | -k | -m | -g | -h] [-o] [-s delay ] [-c count ] [-a] [-t] [ ...
- N天学习一个linux命令之ping
用途 检测主机是否可到达,也就是说,目标主机是否可以联网,还可以用于检测网速.通过发送ICMP ECHO_REQUEST数据包检测. 用法 ping [options] destination 常用选 ...
- N天学习一个linux命令之du
用途 统计文件或者目录占用硬盘空间大小 用法 du [OPTION] [FILE]du [OPTION] --files0-from=F 常用参数 -a, --all统计所有文件,不仅仅是目录 -b, ...
- N天学习一个linux命令之scp
用途 通过ssh通道,不同主机之间复制文件 用法 scp [options] [user@host:]file1 [user2@host2:]file2 常用参数 -1使用 ssh 1协议 -2使用s ...
- 每天学习一个Linux命令-目录
在工作中总会零零散散使用到各种Linux命令,从今天开始详细的学习一下linux常用命令,坚持每天一个命令,学习的主要参考资料为: 1.竹子-博客(https://www.cnblogs.com/pe ...
- 每天一个linux命令(51)--grep命令
linux系统中grep 命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来.grep 全称是 global regular expression print,表示全局正则 ...
- N天学习一个linux命令之umask
前言 umask不是linux命令,而是shell内置的指令,俗称用户权限掩码,用于对用户创建的文件和目录设置默认权限.默认的权限掩码是0022,也就是说新创建的文件权限是0644,新创建的目录权限是 ...
随机推荐
- 图练习-BFS-从起点到目标点的最短步数
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2830 简单bfs #include <s ...
- [App Store Connect帮助]三、管理 App 和版本(2.1)输入 App 信息:查看和编辑 App 信息
在您添加 App 至您的帐户后,您也可以在“我的 App”部分查看和编辑 App 信息和平台版本信息. 在输入 App 信息前,请检查必填项.可本地化和可编辑属性.您在上传构建版本或提交您的 App ...
- [Swift通天遁地]五、高级扩展-(8)ImageView(图像视图)的各种扩展方法
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- AcWing算法基础1.5
前缀和与差分 两个内容都比较少,就放一起写了 设数组 a 的前 n 项为a1 , a2 , a3 ... an 前缀和数组就是每一项是a数组的前i项和,比如前缀和数组res,res[ 1 ] = a[ ...
- 悼念512汶川大地震遇难同胞——老人是真饿了 hdu 2187
在此对 曾经 努力参加 救援的人 致以深深的敬意 . 这一道题 挺简单的 就是简单的 结构体+贪心 而已 不过 用英文 注释 是一个 很大的 进步 , 以后 要习惯 http://acm.hdu ...
- BZOJ 4828 DP+BFS
被一道简单BFS坑了这么长时间我也是hhh了 //By SiriusRen #include <bits/stdc++.h> using namespace std; ,,):d(D),x ...
- 【BZOJ3110】[ZJOI2013]K大数查询(整体二分)
题目: BZOJ3110 分析: 整体二分模板题-- 先明确一下题意:每个位置可以存放多个数,第一种操作是"加入 (insert) "一个数而不是"加上 (add) &q ...
- 344 Reverse String 反转字符串
请编写一个函数,其功能是将输入的字符串反转过来.示例:输入:s = "hello"返回:"olleh"详见:https://leetcode.com/probl ...
- 海量文本信息查Top-k
问题描述: 有1千万条短信,一条一行,有重复.在5分钟之内,找出重复出现的前10条. 方案一: 1.分组进行边扫描边建散列表.建立哈希表,使用头,尾和中间随便两个字节作为Hash Code, 插入到H ...
- Hadoop基础(二)
HDFS 读写流程 我们知道在HDFS中我们的文件按数据块进行存储,那么当我们写入或者读取一个文件的时候HDFS到底进行了哪些操作呢? HDFS 写流程 如上图所示,假如我们有一个四个节点的集群,并且 ...