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,新创建的目录权限是 ...
随机推荐
- Is the Information Reliable?(差分约束系统)
http://poj.org/problem?id=2983 题意:给出M条信息,判断这些信息的正确性.(1)V A B :表示A,B之间的距离>=1; (2)P A B X :表示A B之间的 ...
- 利用AXIS2传递JSON数据
Axis2是目前比较流行的WebService引擎.WebService被应用在很多不同的场景.例如,可以使用WebService来发布服务端 Java类的方法,以便使用不同的客户端进行调用.这样可以 ...
- hdu2027
http://acm.hdu.edu.cn/showproblem.php?pid=2027 #include<iostream> #include<stdio.h> #inc ...
- Mac OS安装octave出现的问题-'error:terminal type set to 'unknown'的解决'
学习Machine learning需要使用Octave语言,毕竟Andrew Ng (恩达.吴)力荐.本机系统Mac OS X EI Capitan, 其实什么系统都无所谓了,安装原理都是一样的. ...
- 回收maven私仓过期垃圾
login->scheduled tasks->add
- 如何下载 Nginx (windows 版本)并且简单的使用
官网地址:http://nginx.org/ 进到官网 我这里下载的是 稳定版的 windows版本. 开始我们的简单测试 步骤一:找到nginx的压缩包,(随意找个地方)解压 步骤二:进入conf文 ...
- linq 分组
var data = from r in listRecords group r by new { r.CampaignId, r.CityId, r.Gift_DistributorId, r.Pr ...
- 解决Sql Server 日志满了,设置收缩
解决Sql Server 日志满了,设置收缩: --查看文件占用空间 . '文件大小(MB)',* from sysfiles; ALTER DATABASE SpyData SET RECOVERY ...
- [Windows Server 2012] PHPWind安全设置
★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★[护卫神·V课堂]是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:PHPWin ...
- VC++文件监控 ReadDirectoryChangesW
#include <windows.h> #include <tchar.h> #include <stdio.h> #include <assert.h&g ...