日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

Linux 实操 —— 日志筛选操作(sed与wc命令介绍)

發(fā)布時(shí)間:2025/3/12 55 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux 实操 —— 日志筛选操作(sed与wc命令介绍) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

引言

在處理數(shù)據(jù)交互業(yè)務(wù)場(chǎng)景的問(wèn)題時(shí),經(jīng)常會(huì)出現(xiàn)需要統(tǒng)計(jì)日志中出現(xiàn)某些指定關(guān)鍵字的行數(shù),或者行數(shù),那么如何通過(guò)Linux 命令來(lái)快速統(tǒng)計(jì)一段時(shí)間之內(nèi),某個(gè)特定關(guān)鍵字出現(xiàn)的行數(shù)呢?這篇文章將會(huì)給你答案。

一、快速統(tǒng)計(jì)一段時(shí)間內(nèi)特定log關(guān)鍵字出現(xiàn)行數(shù)

指令示例:

sed -n '/2019-12-28 11:26/,/2019-12-28 12:13/p' nohup.out | grep '接收到xx請(qǐng)求參數(shù),開(kāi)始處理' | wc -l

上面的命令中,'接收到xx請(qǐng)求參數(shù),開(kāi)始處理' 即特定關(guān)鍵字。兩個(gè)時(shí)間代表篩選日志的起止位置,后面會(huì)詳細(xì)介紹。

通常做這種操作的目的,就是為了根據(jù) log 出現(xiàn)的行數(shù),判斷收到了多少次請(qǐng)求,從而進(jìn)行分析和比較,比如和數(shù)據(jù)庫(kù)中記錄的條數(shù)進(jìn)行比對(duì),檢查是否有丟包或異常未入庫(kù)的情況。在實(shí)際排查問(wèn)題的時(shí)候經(jīng)常會(huì)用到。

注意,上述命令中用于篩選的日期一定是日志中真正出現(xiàn)的時(shí)間,否則無(wú)效,可以不精確,省略時(shí)間尾部即可。另外,我們一定要通過(guò)代碼來(lái)確定某個(gè) log 相對(duì)于每次請(qǐng)求是唯一的,如果一次請(qǐng)求中出現(xiàn)多次相同關(guān)鍵字 ,那么統(tǒng)計(jì)結(jié)果會(huì)比實(shí)際請(qǐng)求的行數(shù)要多。

二、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 是一個(gè)流編輯器。流編輯器是使用輸入流(文件或管道輸入)來(lái)處理文本文件轉(zhuǎn)化的工具。在某些方面有點(diǎn)像支持腳本編輯的編輯器(例如 ed),sed 僅基于一次輸入來(lái)工作,因此也更高效。但真正與其他種類(lèi)編輯器相區(qū)分的是 sed 的過(guò)濾文本的能力。

常用參數(shù)和基本語(yǔ)法:

sed [-hnV][-e<script>][-f<script文件>][文本文件]

參數(shù)說(shuō)明

  • -e<script>或--expression=<script> 以選項(xiàng)中指定的script來(lái)處理輸入的文本文件。
  • -f<script文件>或--file=<script文件> 以選項(xiàng)中指定的script文件來(lái)處理輸入的文本文件。
  • -h或--help 顯示幫助。
  • -n或--quiet或--silent 僅顯示script處理后的結(jié)果,屏蔽默認(rèn)輸出(全部文本)。
  • -V或--version 顯示版本信息。

sed 中的定址符與正則表達(dá)式

在命令:sed -n '/2019-12-28 11:26/,/2019-12-28 12:13/p' nohup.out 中,用到了定址符,定址符用來(lái)定義需要操作的文本的起止位置,由 '地址1,地址2' 組成,地址可以表示為文本的行號(hào),也可以使用正則,上面的命令用到的是正則的方式,行號(hào)的方式如下:

1、sed -n '5,10p'? ? ? 輸出第 5 - 10 行的日志

2、sed -n '5p,10p'? ? 輸出第5、第10行日志

p 表示基本的處理動(dòng)作是打印 print 。另外還有 d 刪除,s 替換字符串。

常用操作:數(shù)據(jù)的搜索顯示

示例來(lái)自菜鳥(niǎo)教程(更多內(nèi)容):搜索 /etc/passwd有root關(guān)鍵字的行:

> 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

(補(bǔ)充:nl?(Number of Lines) 將指定的文件添加行號(hào)標(biāo)注后寫(xiě)到標(biāo)準(zhǔn)輸出。如果不指定文件或指定文件為"-" ,程序?qū)臉?biāo)準(zhǔn)輸入讀取數(shù)據(jù)。)

上述示例,如果找到 root ,不僅會(huì)輸出匹配行,也會(huì)輸出所有行。為了只輸出匹配的行,必須要加上 -n 參數(shù)。因此在一般的搜索場(chǎng)景下,-n 基本都是必選參數(shù)

三、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.

打印每個(gè)文件的換行、字和字節(jié)的數(shù)量,指定多個(gè)文件則為所有行。如果沒(méi)有文件,或文件是 “-”?,就從標(biāo)準(zhǔn)輸入中讀取。字是由空格分隔的長(zhǎng)度非0的字符序列。下面的選項(xiàng)可以用于選擇打印哪種計(jì)數(shù),通常按照下面順序打印:行數(shù)、字?jǐn)?shù)、字符數(shù)、字節(jié)數(shù)、最大行數(shù)。

簡(jiǎn)單來(lái)說(shuō),wc 就是輸出文本內(nèi)容的統(tǒng)計(jì)信息。

常用參數(shù):

  • -c 或--bytes或--chars 只顯示Bytes數(shù)。
  • -l 或--lines 只顯示行數(shù)。
  • -w 或--words 只顯示字?jǐn)?shù)。
  • --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

也可以統(tǒng)計(jì)多個(gè)文件:

> wc testwc.txt wcFile.json 7 92 608 testwc.txt3 34 244 wcFile.json10 126 852 total

參考資料

《Linux sed 命令》

《Linux wc 命令》

《sed命令詳解+示例》

《Linux日志篩選命令》

總結(jié)

以上是生活随笔為你收集整理的Linux 实操 —— 日志筛选操作(sed与wc命令介绍)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。