我使用sed / awk来解析mq runmqsc输出。我们希望某些字段显示在一行。这似乎是一个简单的sed / awk问题。
<!-- main containing table -->
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="center">
<!-- main body table -->
<table class="w320" width="640" cellpadding="0" cellspacing="0" border="0" style="position:relative;">
<tr>
<td style="position:relative;>
<!--[if gte mso 9]-->
<v:image src="...yourimagehere.jpg" style="width:203px;height:432px;position:absolute;top:0;right:0;z-index:-1;" />
<![endif]-->
Your email here
5724-H72(C)版权所有IBM Corp. 1994,2009。保留所有权利。 启动队列管理器的MQSC。
echo "display conn(*) ALL" |
runmqsc <BrokerName> |
awk '{ RS = "AMQ8276: Display Connection details." } ; { print $0 }' |
sed -e 's/( )/()/g'
此代码:
1 : display conn(*) ALL
CONN(3923A95601000020)
EXTCONN(414D51435465737442726F6B65725553)
TYPE(CONN)
PID(9263) TID(1)
APPLDESC(WebSphere MQ Object Authority Manager)
APPLTAG(amqzfuma) APPLTYPE(SYSTEM)
ASTATE(NONE) CHANNEL()
CONNAME() CONNOPTS(MQCNO_FASTPATH_BINDING)
USERID(mqm) UOWLOG()
UOWSTDA() UOWSTTI()
UOWLOGDA() UOWLOGTI()
URTYPE(QMGR)
EXTURID(XA_FORMATID[00000000] XA_GTRID[] XA_BQUAL[])
QMURID(0.0) UOWSTATE(NONE)
返回:
echo "display conn(*) ALL" | runmqsc TestBrokerUS | awk '{ RS = "AMQ8276: Display Connection details." } ; { print $0 }' | sed -e 's/( )/()/g' |
sed -n -e 's/^.* CHANNEL(\(.*\).*) /\1/p' -e 's/^.* USERID(\(.*\).*)/\1/p' -e 's/^.* CONNOPTS(\(.*\).*)/\1/p' -e 's/^.* CONN(\(.*\).*)/\1/p' -e 's/^.* CONNAME(\(.*\).*)/\1/p'
需要在1行解析这些字段。想法?
答案 0 :(得分:1)
一种选择是从输出中替换换行符,然后为每个dis conn添加一个换行符。
示例:
echo "dis conn(*) all" | runmqsc MQ8QMGR|sed ':a;N;$!ba;s/\n/ /g'|sed 's/UOWSTATE/\n/g'
其他选项是将值存储在变量中,然后在获取最后一个属性时进行打印。这是一个解析dis qs输出的例子。
/QUEUE/{i1=index($1,"("); i2=index($1,")")-1; QUEUE=substr($1,i1+1,i2-i1);}
/CURDEPTH/{i1=index($1,"("); i2=index($1,")")-1; CURDEPTH=substr($1,i1+1,i2-i1);
i3=index($2,"("); i4=index($2,")")-1; IPPROCS=substr($2,i3+1,i4-i3);
}
/LGETDATE/{i1=index($1,"("); i2=index($1,")")-1; LGETDATE=substr($1,i1+1,i2-i1);
i3=index($2,"("); i4=index($2,")")-1; LGETTIME=substr($2,i3+1,i4-i3);
}
/LPUTDATE/{i1=index($1,"("); i2=index($1,")")-1; LPUTDATE=substr($1,i1+1,i2-i1);
i3=index($2,"("); i4=index($2,")")-1; LPUTTIME=substr($2,i3+1,i4-i3)
}
/MSGAGE/{i3=index($2,"("); i4=index($2,")")-1; MSGAGE=substr($2,i3+1,i4-i3);}
/OPPROCS/{i1=index($1,"("); i2=index($1,")")-1; OPPROCS=substr($1,i1+1,i2-i1);
i3=index($2,"("); i4=index($2,",")-1; QTIME=substr($2,i3+1,i4-i3);
i5=1; i6=index($3,")")-1; QTIME2=substr($3,i5,i6-i5+1);
}
/UNCOM/{
i1=index($1,"("); i2=index($1,")")-1; UNCOM=substr($1,i1+1,i2-i1);
if( QUEUE == "SYSTEM.CLUSTER.TRANSMIT.QUEUE" )
{
print date "|" QUEUE "|" CURDEPTH "|" LGETDATE "|" LGETTIME "|" LPUTDATE "|" LPUTTIME "|" MSGAGE "|" QTIME "," QTIME2 "|" UNCOM;
}
}
答案 1 :(得分:0)
我使用以下脚本:
#! /usr/bin/env gawk -f
#
# Parses output of 'runmqsc' command and prints a comma separated list of the
# attributes on one line per object.
#
# CAVEAT: Fails if '(' or ')' characters are in attribute values (e.g. DESC)
#
# Daniel Steinmann, Mai 2015
function ltrim(s) { sub(/^[ \t\r\n]+/, "", s); return s }
function rtrim(s) { sub(/[ \t\r\n]+$/, "", s); return s }
function trim(s) { return rtrim(ltrim(s)); }
BEGIN {
RS = "AMQ[0-9]+: [^.]*."
FS = "[()]"
}
{
# Filter out header
if (NR == 1) next
for (i = 1; i <= NF; i = i+2) {
key = trim($i)
value = trim($(i+1))
# Filter out empty lines and trailer
if (key ~ /[ ]/ || key == "") continue
if (i > 1) printf(", ")
printf("%s(%s)", key, value)
}
printf("\n")
}
它产生如下输出:
$ echo "display conn(*) all | runmqsc MYQMGR | ./parse_runmqsc_output.gawk
CONN(F290EE5720001801), EXTCONN(414D51435858582E534232492E4D4149), ...
CONN(F290EE572009F312), EXTCONN(414D51435858582E534232492E4D4149), ...
您可以轻松调整gawk
脚本以按您希望的方式生成输出。