我有一些如下所示的输出:
Gathering data for drive 1 ...
Drive name: id1,sd@n5000cca17096
Drive Model: HUH721010AL4204
Drive Speed: 7200 RPMs
Drive Temp: 41 C
Gathering data for drive 2 ...
Drive name: id1,sd@n5000cca24156
Drive Model: HUH721010AL4204
Drive Speed: 7200 RPMs
Drive Temp: 41 C
Gathering data for drive 3 ...
Drive name: id1,sd@n5000cca8749
Drive Model: HUH721010AL4204
failed to get drive stats
Gathering data for drive 4 ...
Drive name: id1,sd@n5000cca19183
Drive Model: HUH721010AL4204
Drive Speed: 7200 RPMs
Drive Temp: 41 C
Gathering data for drive 5 ...
Drive name: id1,sd@n5000cca4607
Drive Model: HUSMH8010BSS204
failed to get drive stats
Gathering data for drive 6 ...
Drive name: id1,sd@n5000cca10152
Drive Model: HUH721010AL4204
Drive Speed: 7200 RPMs
Drive Temp: 41 C
我想为无法返回驱动器统计信息的任何驱动器打印“驱动器名称”(因此匹配 on failed to get drive stats
)。
我知道如何获得上一行:
awk '$0=="failed to get drive stats" && $2>1 {print f} {f=$0}'
但这对我没有帮助。
期望的输出:
Drive name: id1,sd@n5000cca8749
Drive name: id1,sd@n5000cca4607
编辑:
由于某种原因,当输出被重定向时,它的显示方式与终端上的不同。如果我运行:
command >out.txt 2>&1
输出文件将如下所示:
failed to get drive stats
failed to get drive stats
Gathering data for drive 1 ...
Drive name: id1,sd@n5000cca17096
Drive Model: HUH721010AL4204
Drive Speed: 7200 RPMs
Drive Temp: 41 C
Gathering data for drive 2 ...
Drive name: id1,sd@n5000cca24156
Drive Model: HUH721010AL4204
Drive Speed: 7200 RPMs
Drive Temp: 41 C
....
两个 awk 答案都提供了工作,但我猜我还有其他问题
这首先将输入字段分隔符设置为 a
:
。saved
然后,只要找到指定驱动器名称的行,它就会将该行保存在变量中。如果一行显示 ,则打印
failed to get drive stats
的当前值。saved
这就是 awk 具有“段落模式”的原因,您所需要的只是:
尝试使用以下方法,效果很好
输出