过滤输出

过滤输出
for i in $(cat HPE_FRAMES_IP); do  ssh Reports@$i showsys | awk '{print $2,$5}' | column -t ; ssh Reports@$i showpd -degraded -failed | awk '{print $1,$2,$3,$5}' ; done | grep -v "MiB" | grep -v "total"
--Name---  Nodes
AN4ZX8401  MXN6232L4X
No PDs listed
----Name-----  Nodes
bga1ZX84-3128  4C17253128
No PDs listed
---Name---  Nodes
ZZ2ZX20841  CZ3741XP9X
No PDs listed
--Name---  Nodes
ZZ2ZX8401  MXN6102C7F
No PDs listed
--Name---  Nodes
ZZ2ZX8402  2MT814A12A
No PDs listed
--Name---  Nodes
CR3ZX8401  2M263601BJ
Id CagePos Type State
175 9:15:0 FC failed
---Name---  Nodes
ZZZZX20841  CZ3741X5K8
Id CagePos Type State
18 4:2:0 SSD degraded
-------Name-------  Nodes
ZZZZX8401_Full_DNA  MXN54823JY
No PDs listed
--Name---  Nodes
ZZZZX8402  MXN6102CAF
No PDs listed
-------Name-------  Nodes
ZZZZX8403_Full_DNA  MXN6192JEW
No PDs listed

以下代码提供了所需的输出,但是我需要使用以下格式将此输出重定向到 CSV 文件中

我只想在 showpd 和 showsys 命令的实际输出已降级或失败时才打印输出。

期望的输出应该是:

--Name---,Nodes
CR3ZX8401,2M263601BJ
Id,CagePos,Type,State
175,9:15:0,FC,failed

---Name---,Nodes
ZZZZX20841,CZ3741X5K8
Id,CagePos,Type,State
18,4:2:0,SSD,degraded

问题更新如下

showpd 输出可以有多个降级和失败的输出,在这种情况下,使用 -B3 打印将不起作用...如果输出以下模式怎么办

'---Name---,Nodes 
FM1HP20841,CZ3741X5K8 
Id,CagePos,Type,State 
18,4:2:0,SSD,degraded 
18,4:2:1,SSD,failed 

答案1

尝试这个,

for i in $(cat HPE_FRAMES_IP); do ssh Reports@$i showsys | awk '{print $2","$5}' ; ssh Reports@$i showpd -degraded -failed | awk '{print $1","$2","$3","$5}' ; done | grep -v "MiB" | grep -v "total" | egrep -B3 "failed|degraded" | sed 's/---Name/\n---Name/g' > file.csv
  • 只需将逗号用双引号括起来awk
  • 消除column
  • 然后使用关键字进行扩展 grep
  • -B3将打印匹配关键字之前的 3 行
  • sed是在“Name”之前添加新行作为预期输出

答案2

Grep 将在这里提供帮助:

for i in $(cat HPE_FRAMES_IP); do  ssh Reports@$i showsys | awk '{print $2,$5}' | column -t ; ssh Reports@$i showpd -degraded -failed | awk '{print $1,$2,$3,$5}' ; done | grep -v "MiB" | grep -v "total" | grep -B 4 -iE "failed|degraded"

修改:

通过管道输出grep -B 4 -iE“失败|降级”

相关内容