grep -v 和 -B|-A|-C 不能一起工作

发布于 2024-09-12 20:35:06 字数 1829 浏览 5 评论 0原文

我需要在 Solaris 计算机上扫描 iostat -En 的输出,以便在发现有错误的磁盘时,将有关该磁盘的两行发送到最终输出。到目前为止使用效果很好,

iostat -En | grep 'Errors: [1-9]' -A 1

但是当我尝试不包含 CD/DVD 设备时,我遇到了困难。 contaxt 标志 (-A|B|C) 似乎不适用于 -v 所以我不能使用这个:

-bash-4.0$ iostat -En | grep -B 1 -vi "CD/DVD" | grep 'Errors: [1-9]' -A 1
c0t3d0           Soft Errors: 2 Hard Errors: 0 Transport Errors: 0
Vendor: TSSTcorp Product: CD/DVDW TS-L632D Revision: SR02 Serial No:

我认为不应该此处的任何输出,因为包含“CD/DVD”的行及其之前的行应被删除。用任何语言将其编写为脚本都足够简单,但它必须是一个可以在主机上运行而无需安装其他脚本的命令,因此我认为可以使用 perl -e 。我不了解 perl,所以我没有尝试过这种方法。

一些示例 iostat -En 输出:

-bash-4.0$ iostat -En
c0t0d0           Soft Errors: 0 Hard Errors: 0 Transport Errors: 0
Vendor: SEAGATE  Product: ST973402SSUN72G  Revision: 0603 Serial No: *removed*
Size: 73.40GB <73400057856 bytes>
Media Error: 0 Device Not Ready: 0 No Device: 0 Recoverable: 0
Illegal Request: 0 Predictive Failure Analysis: 0
c0t3d0           Soft Errors: 2 Hard Errors: 0 Transport Errors: 0
Vendor: TSSTcorp Product: CD/DVDW TS-L632D Revision: SR02 Serial No:
Size: 0.00GB <0 bytes>
Media Error: 0 Device Not Ready: 0 No Device: 0 Recoverable: 0
Illegal Request: 2 Predictive Failure Analysis: 0
c0t1d0           Soft Errors: 0 Hard Errors: 0 Transport Errors: 0
Vendor: SEAGATE  Product: ST973402SSUN72G  Revision: 0603 Serial No: *removed*
Size: 73.40GB <73400057856 bytes>
Media Error: 0 Device Not Ready: 0 No Device: 0 Recoverable: 0
Illegal Request: 0 Predictive Failure Analysis: 0

在这种情况下,我不应该有任何输出,因为唯一有错误的设备是光盘驱动器,而我不关心这些。

c0t1d0           Soft Errors: 0 Hard Errors: 1 Transport Errors: 0
Vendor: SEAGATE  Product: ST973402SSUN72G  Revision: 0603 Serial No: *removed*

这就是我正在寻找的输出。有什么想法吗?

I need to scan the output of iostat -En on a Solaris machine such that when a disk with errors is found, two lines about that disk are sent to the final output. This works fine so far using

iostat -En | grep 'Errors: [1-9]' -A 1

but when I try to not include CD/DVD devices, I hit a wall. The contaxt flags (-A|B|C) don't seem to work with -v so I can't use this:

-bash-4.0$ iostat -En | grep -B 1 -vi "CD/DVD" | grep 'Errors: [1-9]' -A 1
c0t3d0           Soft Errors: 2 Hard Errors: 0 Transport Errors: 0
Vendor: TSSTcorp Product: CD/DVDW TS-L632D Revision: SR02 Serial No:

I don't think there should be any output here because the line containing "CD/DVD" and the line before it should be removed. It would be simple enough to write this as a script in any language but it has to be a single command that can be run on hosts without additional scripts installed so I suppose perl -e can be used. I don't know any perl so I haven't tried this approach.

Some example iostat -En output:

-bash-4.0$ iostat -En
c0t0d0           Soft Errors: 0 Hard Errors: 0 Transport Errors: 0
Vendor: SEAGATE  Product: ST973402SSUN72G  Revision: 0603 Serial No: *removed*
Size: 73.40GB <73400057856 bytes>
Media Error: 0 Device Not Ready: 0 No Device: 0 Recoverable: 0
Illegal Request: 0 Predictive Failure Analysis: 0
c0t3d0           Soft Errors: 2 Hard Errors: 0 Transport Errors: 0
Vendor: TSSTcorp Product: CD/DVDW TS-L632D Revision: SR02 Serial No:
Size: 0.00GB <0 bytes>
Media Error: 0 Device Not Ready: 0 No Device: 0 Recoverable: 0
Illegal Request: 2 Predictive Failure Analysis: 0
c0t1d0           Soft Errors: 0 Hard Errors: 0 Transport Errors: 0
Vendor: SEAGATE  Product: ST973402SSUN72G  Revision: 0603 Serial No: *removed*
Size: 73.40GB <73400057856 bytes>
Media Error: 0 Device Not Ready: 0 No Device: 0 Recoverable: 0
Illegal Request: 0 Predictive Failure Analysis: 0

In this case, I should not have any output because the only device with errors is an optical drive and I don't care about those.

c0t1d0           Soft Errors: 0 Hard Errors: 1 Transport Errors: 0
Vendor: SEAGATE  Product: ST973402SSUN72G  Revision: 0603 Serial No: *removed*

This is the output I am looking for. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔日梦未散 2024-09-19 20:35:06

尝试nawk

iostat -En | nawk '/Errors: [1-9]/{ getline line; if (line!~/CD|DVD/) print line}

try nawk

iostat -En | nawk '/Errors: [1-9]/{ getline line; if (line!~/CD|DVD/) print line}
三寸金莲 2024-09-19 20:35:06

grep -v pattern 打印所有与模式不匹配的行,
但是 grep -B 1 -v pattern 还会打印出与模式不匹配的行之前的所有行,包括与模式匹配的行。

例如,如果 file 包含:

ABCD
DEFG
GHIJ

那么 grep -v D file 为您提供:

GHIJ

grep -B1 -v D file 为您提供

DEFG
GHIJ

:对于 grep 来说,你想要的处理太复杂了。

grep -v pattern prints all lines that don't match a pattern,
but grep -B 1 -v pattern also prints out all lines before lines that don't match a pattern, including lines that do match the pattern.

For example, if file contains:

ABCD
DEFG
GHIJ

Then grep -v D file gives you:

GHIJ

but grep -B1 -v D file gives you:

DEFG
GHIJ

The kind of processing you want is too complex for grep.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文