如何使用awk或sed将块样式记录格式化为表格?

时间:2013-12-07 23:44:37

标签: bash sed awk

我正在尝试将块式记录转换为表格。该记录是有关磁盘的AIX ODM信息。

--------------------------------------------------------
lsattr -El hdisk2 :-

pvid           00f68614bd9b2da20000000000000000 Physical volume identifier       False
q_err          no                 Use QERR bit                     True
q_type         simple             Queue TYPE                       True
queue_depth    16                 Queue DEPTH                      True
reserve_lock   no                 Reserve Device on open           True
reserve_policy single_path        N/A                              True
rw_timeout     40                 READ/WRITE time out value        True
scsi_id        0x71003f           SCSI ID                          False
start_timeout  180                START UNIT time out value        True
ww_name        0x5000097208426d54 FC World Wide Name               False
--------------------------------------------------------
lsattr -El hdisk3 :-

pvid           00f686145f0caa790000000000000000 Physical volume identifier       False
q_err          no                               Use QERR bit                     True
q_type         simple                           Queue TYPE                       True
queue_depth    16                               Queue DEPTH                      True
reserve_lock   no                               Reserve Device on open           True
reserve_policy single_path                      N/A                              True
rw_timeout     40                               READ/WRITE time out value        True
scsi_id        0x71003f                         SCSI ID                          False
start_timeout  180                              START UNIT time out value        True
ww_name        0x5000097208426d54               FC World Wide Name               False
--------------------------------------------------------

所需的输出是一个列出pvid,reserve_policy和ww_names的表,如:

hdisk2 00f68614bd9b2da20000000000000000 single_path 5000097208426d54
hdisk3 00f686145f0caa790000000000000000 single_path 5000097208426d54

如何用awk或sed做到这一点? 请帮忙。 谢谢

1 个答案:

答案 0 :(得分:1)

这一行给出了你想要的东西:

awk 'BEGIN{RS="---+\n";a["pvid"];a["reserve_policy"];a["ww_name"]}{printf "%s",$3;for(i=1;i<=NF;i++)if($i in a)printf " %s", $(i+1);print ""}' file

更易阅读的版本:

awk 'BEGIN{RS="---+\n"
           a["pvid"]
           a["reserve_policy"]
           a["ww_name"]}
{printf "%s",$3
 for(i=1;i<=NF;i++)
     if($i in a)printf " %s", $(i+1)
 print ""}' file