Bash使用过滤器解析命令的输出

时间:2013-04-30 21:20:28

标签: bash awk

我有一个输出硬盘状态的命令。

我打算在脚本中运行它以进行监控。 我想拉出某些行并显示它们,例如Slot Number,PD type Raw Size,Drive的位置。

我该怎么做? (我假设它会是某种awk声明。) 输出是这样的(注意“(\ n)”表示新行而不是格式选择)

Enclosure Device ID: 252
Slot Number: 3
Drive's postion: DiskGroup: 1, Span: 0, Arm: 1
Enclosure position: 0
Device Id: 7
WWN: 5000C50034BB0CD8
Sequence Number: 2
Other Error Count: 0
Predictive Failure Count: 0
Last Predictive Failure Event Seq Number: 0
PD Type: SAS
Raw Size: 2.728 TB [0x15d50a3b0 Sectors]
Non Coerced Size: 2.728 TB [0x15d40a3b0 Sectors]
Coerced Size: 2.727 TB [0x15d3ef000 Sectors] 
Firmware state: Online, Spun Up
Device Firmware Level: 0003
Connected Port Number: 2(path0)
Inquiry Data: SEAGATE ST33000650SS    0003Z290VK2V
(\n)
(\n)
(\n)
Enclosure Device ID: 252
Slot Number: 4
Drive's postion: DiskGroup: 1, Span: 0, Arm: 1
Enclosure position: 0
Device Id: 8
WWN: 5000C50034BB0CD8
Sequence Number: 2
Other Error Count: 0
Predictive Failure Count: 0
Last Predictive Failure Event Seq Number: 0
PD Type: SAS
Raw Size: 2.728 TB [0x15d50a3b0 Sectors]

(继续这样)

修改

我想将它们显示为

Slot Number: 3
Drive's postion: DiskGroup: 1, Span: 0, Arm: 1
Enclosure position: 0
Device Id: 7
PD Type: SAS
Raw Size: 2.728 TB [0x15d50a3b0 Sectors]ors] 
Firmware state: Online, Spun Up
Device Firmware Level: 0003
(\n)
Slot Number: 4
Drive's postion: DiskGroup: 1, Span: 0, Arm: 1
Enclosure position: 0
Device Id: 8
PD Type: SAS
Raw Size: 2.728 TB [0x15d50a3b0 Sectors]ors] 
Firmware state: Online, Spun Up
Device Firmware Level: 0003

1 个答案:

答案 0 :(得分:0)

如果您只想提取感兴趣的行,可以使用egrep

cmd | egrep '^((Slot Number)|(PD Type)|(Raw Size)):' \
    | sed 's/^Slot Number/\n&/'