linux通过grep根据关键字查找日志文件上下文
linux通过grep根据关键字查找日志文件上下文
1、在标准unix/linux下的grep命令中,通过以下参数控制上下文的显示:
grep -C 10 keyword catalina.out 显示file文件中匹配keyword字串那行以及上下10行
grep -B 10 keyword catalina.out 显示keyword及前10行
grep -A 10 keyword catalina.out 显示keyword及后10行
2、查找的结果比较大时,为了方便定位问题,也可以重定向到文件中,比如:
grep -C 10 keyword catalina.out > aaa.txt
3、统计包含某个关键字的个数
grep -o keyword catalina.out | wc -l
4、如果一行最多一个关键字,可以简写为:
grep -c keyword catalina.out
5、查看grep版本的方法是
grep -V
Grep命令详细说明:
Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE or standard input.
PATTERN is, by default, a basic regular expression (BRE).
Example: grep -i 'hello world' menu.h main.c
Regexp selection and interpretation:
-E, --extended-regexp PATTERN is an extended regular expression (ERE)
-F, --fixed-strings PATTERN is a set of newline-separated fixed strings
-G, --basic-regexp PATTERN is a basic regular expression (BRE)
-P, --perl-regexp PATTERN is a Perl regular expression
-e, --regexp=PATTERN use PATTERN for matching
-f, --file=FILE obtain PATTERN from FILE
-i, --ignore-case ignore case distinctions
-w, --word-regexp force PATTERN to match only whole words
-x, --line-regexp force PATTERN to match only whole lines
-z, --null-data a data line ends in 0 byte, not newline
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
Output control:
-m, --max-count=NUM stop after NUM matches
-b, --byte-offset print the byte offset with output lines
-n, --line-number print line number with output lines
--line-buffered flush output on every line
-H, --with-filename print the file name for each match
-h, --no-filename suppress the file name prefix on output
--label=LABEL use LABEL as the standard input file name prefix
-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-recursive
likewise, but follow all symlinks
--include=FILE_PATTERN
search only files that match FILE_PATTERN
--exclude=FILE_PATTERN
skip 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
Context control:
-B, --before-context=NUM print NUM lines of leading context
-A, --after-context=NUM print NUM lines of trailing context
-C, --context=NUM print NUM lines of output context
-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' means 'grep -E'. 'fgrep' means 'grep -F'.
Direct invocation as either 'egrep' or 'fgrep' is deprecated.
When FILE is -, read standard input. With no FILE, read . if a command-line
-r is given, - otherwise. If fewer than two FILEs are given, assume -h.
Exit status is 0 if any line is selected, 1 otherwise;
if any error occurs and -q is not given, the exit status is 2.
Report bugs to: bug-grep@gnu.org
GNU Grep home page: <http://www.gnu.org/software/grep/>
General help using GNU software: <http://www.gnu.org/gethelp/>
linux通过grep根据关键字查找日志文件上下文的更多相关文章
- Linux显示以log结尾的日志文件
Linux显示以log结尾的日志文件 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ find -name "*.log" find: `. ...
- Linux 查看磁盘容量、查找大文件、查找大目录
Linux 查看磁盘容量.查找大文件.查找大目录 磁盘统计 查看磁盘使用情况 df -h 文件统计 查找/home 目录下大于800M的文件 find /home -type f -size +800 ...
- LINUX中,find结合grep正则表达式,快速查找代码文件。
###目的###LINUX中,find结合grep正则表达式快速查找代码. 例如经常有需求:查找当前目录下所有.h文件中,"public开头,中间任意字符,以VideoFrameReceiv ...
- linux之使用cron,logrotate管理日志文件
1) logrotate配置 logrotate 程序是一个日志文件管理工具.用来把旧的日志文件删除,并创建新的日志文件,我们把它叫做“转储”. 我们可以根据日志文件的大小,也可以根据其天数来 ...
- Linux批量清空当前目录中的日志文件
在Linux中,有时需要批量清空当前目录中的日志文件,同时还要保留日志文件. 其实一行shell命令就可以搞定,一起来看看吧. 在当前目录下,键入如下命令: for i in `find . -nam ...
- linux使用grep和find查找内容
1,在某个路径下查文件. 在/etc下查找“*.log”的文件 find /etc -name “*.log” 2,扩展,列出某个路径下所有文件,包括子目录. find /etc -name “*” ...
- Default Trace 查找日志文件快速增长的原因
select loginname, loginsid, spid, hostname, applicationname, servername, databasename, objectname, e ...
- linux后台启动springboot并指定日志文件名称
如果用nohup启动jar包的话,默认的日志文件就是nohup.out,那么如果启动多个jar包的话,看日志文件就麻烦了,因为他们都会写入到nohup.out文件中. 所以我们来指定一下不同jar包的 ...
- Linux /var/log下的各种日志文件详解
1)/var/log/secure:记录登录系统存取数据的文件;例如:pop3,ssh,telnet,ftp等都会记录在此. 2)/var/log/wtmp:记录登录这的信息记录,被编码过,所以必须以 ...
随机推荐
- 在h5页面上添加音乐播放
接到需求说要做一个h5轮播图,同时配上背景音乐. Html部分: <!--音乐开始--> <div id="music"> <div id=" ...
- Mybatis实现增删改查(二)
1. 准备 请先完成Mybatis基本配置(一)的基本内容 2. 查询多条商品信息 1)在com.mybatis.dao.PartDao中增加接口函数 public List<PartInfo& ...
- 每天一点点之数据结构与算法 - 应用 - 分别用链表和数组实现LRU缓冲淘汰策略
一.基本概念: 1.什么是缓存? 缓存是一种提高数据读取性能的技术,在硬件设计.软件开发中都有着非广泛的应用,比如常见的CPU缓存.数据库缓存.浏览器缓存等等. 2.为什么使用缓存?即缓存的特点缓 ...
- 洛谷 P2341 [HAOI2006]受欢迎的牛|【模板】强连通分量
题目传送门 解题思路: 先求强联通分量,缩点,然后统计新图中有几个点出度为0,如果大于1个,则说明这不是一个连通图,答案即为0.否则入度为0的那个强连通分量的点数即为答案 AC代码: #include ...
- 连接mysql的各种方式
mysql连接操作是客户端进程与mysql数据库实例进程进行通信.从程序设计角度来说,属于进程通信,常用进程通信包括: 管道.Tcp/Ip 套接字.UNIX域套接字. 1.TCP/IP (1)使用最多 ...
- VEH&VCH
本文99.9%的代码及内容作者:mengwuji 来自:http://www.mengwuji.net/forum.php?mod=viewthread&tid=1371 VEH:向量化异常处 ...
- scrapy练习1
1.建立项目: #建立名为tuto的项目 scrapy startproject tuto 2.进入项目目录: cd tuto 3.建立域名任务: #minyan任务名:后面是任务对应的域名 scra ...
- mysql 启动报错Host name could not be resolved解决办法
mysql 启动报错信息如下: [root@xxx ~]# 2018-01-26 17:06:35 33 [Warning] Host name 'bogon' could not be resolv ...
- MySQL性能管理及架构设计:第1章 实例和故事
1-1 什么决定了电商双11大促的成败 数据库架构 1-2 在双11大促中的数据库服务器 通过监控信息从而确定:哪些因素影响了数据库性能? 1-3 在大促中什么影响了数据库性能 1-4 大表带来的问题 ...
- idea 2018.3.4 破解
我的idea_home=C:\Program Files\\IntelliJ IDEA 2018.3.4\ 1.下载破解文件 链接:https://pan.baidu.com/s/1I2APmk-pj ...