引言
在处理数据交互业务场景的问题时,经常会出现需要统计日志中出现某些指定关键字的行数,或者行数,那么如何通过Linux 命令来快速统计一段时间之内,某个特定关键字出现的行数呢?这篇文章将会给你答案。
一、快速统计一段时间内特定log关键字出现行数
指令示例:
sed -n '/2019-12-28 11:26/,/2019-12-28 12:13/p' nohup.out | grep '接收到xx请求参数,开始处理' | wc -l
上面的命令中,'接收到xx请求参数,开始处理' 即特定关键字。两个时间代表筛选日志的起止位置,后面会详细介绍。
通常做这种操作的目的,就是为了根据 log 出现的行数,判断收到了多少次请求,从而进行分析和比较,比如和数据库中记录的条数进行比对,检查是否有丢包或异常未入库的情况。在实际排查问题的时候经常会用到。
注意,上述命令中用于筛选的日期一定是日志中真正出现的时间,否则无效,可以不精确,省略时间尾部即可。另外,我们一定要通过代码来确定某个 log 相对于每次请求是唯一的,如果一次请求中出现多次相同关键字 ,那么统计结果会比实际请求的行数要多。
二、sed 命令
Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text in a pipeline which particularly distinguishes it from other types of editors.
Sed 是一个流编辑器。流编辑器是使用输入流(文件或管道输入)来处理文本文件转化的工具。在某些方面有点像支持脚本编辑的编辑器(例如 ed),sed 仅基于一次输入来工作,因此也更高效。但真正与其他种类编辑器相区分的是 sed 的过滤文本的能力。
常用参数和基本语法:
sed [-hnV][-e<script>][-f<script文件>][文本文件]
参数说明:
- -e<script>或--expression=<script> 以选项中指定的script来处理输入的文本文件。
- -f<script文件>或--file=<script文件> 以选项中指定的script文件来处理输入的文本文件。
- -h或--help 显示帮助。
- -n或--quiet或--silent 仅显示script处理后的结果,屏蔽默认输出(全部文本)。
- -V或--version 显示版本信息。
sed 中的定址符与正则表达式:
在命令:sed -n '/2019-12-28 11:26/,/2019-12-28 12:13/p' nohup.out 中,用到了定址符,定址符用来定义需要操作的文本的起止位置,由 '地址1,地址2' 组成,地址可以表示为文本的行号,也可以使用正则,上面的命令用到的是正则的方式,行号的方式如下:
1、sed -n '5,10p' 输出第 5 - 10 行的日志
2、sed -n '5p,10p' 输出第5、第10行日志
p 表示基本的处理动作是打印 print 。另外还有 d 删除,s 替换字符串。
常用操作:数据的搜索显示
示例来自菜鸟教程(更多内容):搜索 /etc/passwd有root关键字的行:
> nl /etc/passwd | sed '/root/p'
1 root:x:0:0:root:/root:/bin/bash
1 root:x:0:0:root:/root:/bin/bash
2 daemon:x:1:1:daemon:/usr/sbin:/bin/sh
3 bin:x:2:2:bin:/bin:/bin/sh
4 sys:x:3:3:sys:/dev:/bin/sh
5 sync:x:4:65534:sync:/bin:/bin/sync
(补充:nl (Number of Lines) 将指定的文件添加行号标注后写到标准输出。如果不指定文件或指定文件为"-" ,程序将从标准输入读取数据。)
上述示例,如果找到 root ,不仅会输出匹配行,也会输出所有行。为了只输出匹配的行,必须要加上 -n 参数。因此在一般的搜索场景下,-n 基本都是必选参数。
三、wc 命令
Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified. With no FILE, or when FILE is -, read standard input. A word is a non-zero-length sequence of characters delimited by white space. The options below may be used to select which counts are printed, always in the following order: newline, word, character, byte, maximum line length.
打印每个文件的换行、字和字节的数量,指定多个文件则为所有行。如果没有文件,或文件是 “-” ,就从标准输入中读取。字是由空格分隔的长度非0的字符序列。下面的选项可以用于选择打印哪种计数,通常按照下面顺序打印:行数、字数、字符数、字节数、最大行数。
简单来说,wc 就是输出文本内容的统计信息。
常用参数:
- -c 或--bytes或--chars 只显示Bytes数。
- -l 或--lines 只显示行数。
- -w 或--words 只显示字数。
- --help 在线帮助。
- --version 显示版本信息。
示例:
> nl testwc.txt 1 Linux networks are becoming more and more common, but scurity is often an overlooked 2 issue. Unfortunately, in today’s environment all networks are potential hacker targets, 3 fro0m tp-secret military research networks to small home LANs. 4 Linux Network Securty focuses on securing Linux in a networked environment, where the 5 security of the entire network needs to be considered rather than just isolated machines. 6 It uses a mix of theory and practicl techniques to teach administrators how to install and 7 use security applications, as well as how the applcations work and why they are necesary.
> wc testwc.txt7 92 608 testwc.txt
也可以统计多个文件:
> wc testwc.txt wcFile.json 7 92 608 testwc.txt3 34 244 wcFile.json10 126 852 total
参考资料:
《Linux sed 命令》
《Linux wc 命令》
《sed命令详解+示例》
《Linux日志筛选命令》