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,新创建的目录权限是 ...
随机推荐
- bzoj题目分类
转载于http://blog.csdn.net/creationaugust/article/details/513876231000:A+B 1001:平面图最小割,转对偶图最短路 1002:矩阵树 ...
- C语言过时了?为什么还要推荐每一位程序员都来学一下C语言?
互联网蓬勃发展的时代,有一类人做出了巨大的贡献,这一群人被大家称之为程序员,怎样才能成为一名优秀的程序员呢,为什么每一个程序员都需要学习C语言呢? 就让我来跟大家分享分享: 在学习C/C++或者想 ...
- centos源更新
.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup .下载新的CentOS-Base.r ...
- CSS-类和ID选择器的区别
学习了类选择器和ID选择器,我们会发现他们之间有很多的相似处,是不是两者可以通用呢?我们不要着急先来总结一下他们的相同点和不同点: 相同点:可以应用于任何元素不同点: 1.ID选择器只能在文档中使用一 ...
- 数据清洗——python定位csv中的特定字符位置
之前发过一篇关于定位csv中的特殊字符的,主要是用到了python的自带的函数,近期又遇到了一些新的问题,比如isdigit()的缺点在于不能判断浮点型,以及小数中有多个小数点的情况.发现还是正则表达 ...
- bootstrap图标乱码问题-解决方案
楼主在使用bootstrap时,出现了图标乱码问题,经过多次查找,才解决了问题(最后发现真的是很好解决的问题(^^)) 如果出现乱码 请直接在自己写的CSS中重新引入一下font文件中的字体就好了 @ ...
- postgres外部表如何修改源码适配pg升级
postgres中外部表的应用如下: 但是许多在github上的fdw开源代码都是基于9.3以及9.4版本开发,原作者没有随着pg的版本升级而将外部表扩展升级,那只能靠自己去手动修改源码来让这些扩展能 ...
- 获取qq音乐json数据---某课网音乐app学习
移动端qq音乐地址:https://m.y.qq.com/ .抓取QQ音乐数据 请求首页时,有如下链接,回调了jsonp https://c.y.qq.com/splcloud/fcgi-bin/p. ...
- img图片加载出错处理
img图片加载出错处理 为了美观当网页图片不存在时不显示叉叉图片 当在页面显示的时候,万一图片被移动了位置或者丢失的话,将会在页面显示一个带X的图片,很是影响用户的体验.即使使用alt属性给出了” ...
- 不用float也可以让div横向显示
display: inline-block; vertical-align: top; 就这两个属性,给div设置上,div就不会换行显示啦,而且还不影响横向的其他元素的显示.