【发布时间】:2022-11-13 11:12:54
【问题描述】:
我有一个 AV 日志文件,其中显示了每个扫描的进程的多个值:名称、路径、扫描的文件总数、扫描时间。该文件包含数百个这样的流程条目(下面的示例)和扫描的文件总数和扫描时间我想排序并打印最高(或最长)的值,以便确定哪些进程正在影响系统。我用 grep 尝试了各种方法,但似乎只得到一个按数字顺序运行的列表,当我真正想说的是进程 id:86,扫描时间(ns):12761174 是最高的,然后是进程 id 25,等等. 希望我的解释足够清楚。
Process id: 25
Name: wwww
Path: "/usr/libexec/wwww"
Total files scanned: 42
Scan time (ns): "62416"
Status: Active
Process id: 7
Name: xxxx
Path: "/usr/libexec/xxxx"
Total files scanned: 0
Scan time (ns): "0"
Status: Active
Process id: 86
Name: yyyy
Path: "/usr/libexec/yyyy"
Total files scanned: 2
Scan time (ns): "12761174"
Status: Active
我努力了:
grep -Eo | grep 'Scan time (ns)' '[0-9]+' file | sort
结果是:
file:Scan time (ns): "9391986"
file:Scan time (ns): "9532119"
file:Scan time (ns): "9730650"
file:Scan time (ns): "9743828"
file:Scan time (ns): "9793469"
file:Scan time (ns): "9911768"
我想要实现的是:
Process id 9, Scan time (ns): "34561"
Process id 86, Scan time (ns): "45630"
Process id 25, Scan time (ns): "1256822"
Process id 51, Scan time (ns): "52351290"
Process id 30, Scan time (ns): "90257651"
Process id 19, Scan time (ns): "178764794932"
【问题讨论】:
-
请更新问题以显示您的代码生成的(错误)输出和(正确)预期输出,确保两组输出与提供的示例输入相对应
-
第一个 grep 有什么意义?
-
想删掉,多此一举
-
你从哪里得到 34561?请添加您想要的输出,以准确地将样本输入添加到您的问题中。
-
显然
grep 'Scan time (ns)' '[0-9]+' file不起作用,因为grep默认只接收1 个模式,其余非选项参数是输入文件。如果您希望grep找到多个模式,那么您需要使用-e:grep -e 'Scan time (ns)' -e '[0-9]+' file,或者使用正则表达式模式:grep -P 'Scan time \(ns\)|[0-9]+' file