1

I'm using sed/awk to parse mq runmqsc output. We want to get certain fields displayed on a single line. This seems to be a straightforward sed/awk problem.

echo "display conn(*) ALL" | 
runmqsc <BrokerName> | 
awk '{ RS = "AMQ8276: Display Connection details." } ; { print $0 }' | 
sed -e 's/( )/()/g'

5724-H72 (C) Copyright IBM Corp. 1994, 2009. ALL RIGHTS RESERVED. Starting MQSC for queue manager .

 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)

This code:

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'

Returns this:

3923A95601000020)
)                              CONNOPTS(MQCNO_FASTPATH_BINDING)

Need to parse out these fields on 1 line. Thoughts?

1
  • can't help with the bulk of this, but read up on awk's sub() function. You can add sub(/[(] [)]/,"()", $0) (or gsub(...) if you need global sub) instead of adding sed to the pipeline. Good luck.
    – shellter
    Feb 16, 2016 at 4:26

2 Answers 2

1

One option would be to replace the newlines from the output and then add one newline per each dis conn.

Example:

echo "dis conn(*) all" | runmqsc MQ8QMGR|sed ':a;N;$!ba;s/\n/ /g'|sed 's/UOWSTATE/\n/g' 

Other option would be to store the values in variables and then print when you get the last attribute. Here is an example to parse dis qs output.

/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

I use the following script:

#! /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")
}

It produces output like this:

$ echo "display conn(*) all | runmqsc MYQMGR | ./parse_runmqsc_output.gawk
CONN(F290EE5720001801), EXTCONN(414D51435858582E534232492E4D4149), ...
CONN(F290EE572009F312), EXTCONN(414D51435858582E534232492E4D4149), ...

You can easily adapt the gawk script to produce output the way you want.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.