我有 bash 脚本来收集所有硬件信息,但缺少以下内存信息,所以这就是我想要做的。
以下命令为您提供DIMM
内存模块的状态,
[root@Linux ~]# hpasmcli -s 'show dimm'
DIMM Configuration
------------------
Processor #: 1
Module #: 1
Present: Yes
Form Factor: 9h
Memory Type: DDR3(18h)
Size: 8192 MB
Speed: 1333 MHz
Supports Lock Step: No
Configured for Lock Step: No
Status: Ok
Processor #: 1
Module #: 12
Present: Yes
Form Factor: 9h
Memory Type: DDR3(18h)
Size: 8192 MB
Speed: 1333 MHz
Supports Lock Step: No
Configured for Lock Step: No
Status: Ok
Processor #: 2
Module #: 1
Present: Yes
Form Factor: 9h
Memory Type: DDR3(18h)
Size: 8192 MB
Speed: 1333 MHz
Supports Lock Step: No
Configured for Lock Step: No
Status: Ok
Processor #: 2
Module #: 12
Present: Yes
Form Factor: 9h
Memory Type: DDR3(18h)
Size: 8192 MB
Speed: 1333 MHz
Supports Lock Step: No
Configured for Lock Step: No
Status: DIMM is degraded
想要提取Size:
并Status:
需要在单行中,如下所示
最终输出如下所示。我可以使用其他语言,如 python 或 perl,但我是用 bash 编写的,所以我需要 bash 中的一些东西,我可以执行多个操作for loop
并使用变量来使其工作,但我需要一些简单或简短的单个操作,如sed/awk
.我怎样才能在 sed/awk 中实现这一点?
8192MB - Ok
8192MB - OK
8192MB - OK
8192MB - DIMM is degraded
答案1
一种解决方案不使用grep | awk | sed | awk
,而是仅使用一个awk
:
hpasmcli -s 'show dimm' | awk 'BEGIN { stick=0 } $1 == "Size:" { dimm[++stick] = $2" "$3} $1 == "Status:" { $1=""; status[stick]=$0 } END {for( i in dimm ) { print dimm[i], "-", status[i] } }'
8192 MB - Ok
8192 MB - Ok
8192 MB - Ok
8192 MB - DIMM is degraded
该awk
脚本已重新格式化以提高可读性:
BEGIN {
stick=0
}
$1 == "Size:" {
dimm[++stick] = $2" "$3
}
$1 == "Status:" {
$1=""
status[stick]=$0
}
END {
for( i in dimm ) {
print dimm[i], "-", status[i]
}
}'
答案2
天哪,我想我做到了:)
[root@Linux ~]# hpasmcli -s 'show dimm' | egrep -e Size -e Status | awk -F: '{print $2}' | sed 's/^[ \t]*//' | awk 'NR%2{printf "%s - ",$0;next;}1'
8192 MB - Ok
8192 MB - Ok
8192 MB - Ok
8192 MB - DIMM is degraded
不过我想知道是否有其他方法可以做到这一点,我不会回答我的问题,我会等待其他人插话。