From: Harry on
Hi All

I have an (much simplified) input file like this.

$ cat -n connection.txt
1 AMQ8417: Display Channel Status details.
2 CHANNEL(Channel.ABC) CHLTYPE(SVRCONN)
3 CONNAME(123.45.123.67) CURRENT
4 RQMNAME( ) STATUS(RUNNING)
5 SUBSTATE(RECEIVE) XMITQ( )
6 AMQ8417: Display Channel Status details.
7 CHANNEL(Channel.ABC) CHLTYPE(SVRCONN)
8 CONNAME(123.45.123.67) CURRENT
9 RQMNAME( ) STATUS(RUNNING)
10 SUBSTATE(RECEIVE) XMITQ( )
11 AMQ8417: Display Channel Status details.
12 CHANNEL(Channel.ABC) CHLTYPE(SVRCONN)
13 CONNAME(234.56.345.78) CURRENT
14 RQMNAME( ) STATUS(RUNNING)
15 SUBSTATE(RECEIVE) XMITQ( )
16 AMQ8417: Display Channel Status details.
17 CHANNEL(Channel.ABC) CHLTYPE(SVRCONN)
18 CONNAME(234.56.345.78) CURRENT
19 RQMNAME( ) STATUS(RUNNING)
20 SUBSTATE(RECEIVE) XMITQ( )
21 AMQ8417: Display Channel Status details.
22 CHANNEL(Channel.XYZ) CHLTYPE(RCVR)
23 CONNAME(130.45.81.13) CURRENT
24 RQMNAME(THKF7A) STATUS(RUNNING)
25 SUBSTATE(RECEIVE) XMITQ( )
26 AMQ8417: Display Channel Status details.
27 CHANNEL(Channel.VUW) CHLTYPE(SDR)
28 CONNAME(130.45.81.7(10206)) CURRENT
29 RQMNAME(THKF4ZF) STATUS(RUNNING)
30 SUBSTATE(MQGET) XMITQ(Channel.VUW.XMITQ)

Want to create an awk script that could count the entries.

Sample output.

$ cat result.txt
ChannelType Channel Conname Count
SVRCONN Channel.ABC 123.45.123.67 2
SVRCONN Channel.ABC 234.56.345.78 2
RCVR Channel.XYZ 130.45.81.13 1
SDR Channel.VUW 130.45.81.7(10206) 1

Prefer to sort the output with count descending.
Any help appreciated.

TIA

From: Janis Papanagnou on
On 17/07/10 06:03, Harry wrote:
> Hi All
>
> I have an (much simplified) input file like this.
>
> $ cat -n connection.txt
> 1 AMQ8417: Display Channel Status details.
> 2 CHANNEL(Channel.ABC) CHLTYPE(SVRCONN)
> 3 CONNAME(123.45.123.67) CURRENT
> 4 RQMNAME( ) STATUS(RUNNING)
> 5 SUBSTATE(RECEIVE) XMITQ( )
> 6 AMQ8417: Display Channel Status details.
> 7 CHANNEL(Channel.ABC) CHLTYPE(SVRCONN)
> 8 CONNAME(123.45.123.67) CURRENT
> 9 RQMNAME( ) STATUS(RUNNING)
> 10 SUBSTATE(RECEIVE) XMITQ( )
> 11 AMQ8417: Display Channel Status details.
> 12 CHANNEL(Channel.ABC) CHLTYPE(SVRCONN)
> 13 CONNAME(234.56.345.78) CURRENT
> 14 RQMNAME( ) STATUS(RUNNING)
> 15 SUBSTATE(RECEIVE) XMITQ( )
> 16 AMQ8417: Display Channel Status details.
> 17 CHANNEL(Channel.ABC) CHLTYPE(SVRCONN)
> 18 CONNAME(234.56.345.78) CURRENT
> 19 RQMNAME( ) STATUS(RUNNING)
> 20 SUBSTATE(RECEIVE) XMITQ( )
> 21 AMQ8417: Display Channel Status details.
> 22 CHANNEL(Channel.XYZ) CHLTYPE(RCVR)
> 23 CONNAME(130.45.81.13) CURRENT
> 24 RQMNAME(THKF7A) STATUS(RUNNING)
> 25 SUBSTATE(RECEIVE) XMITQ( )
> 26 AMQ8417: Display Channel Status details.
> 27 CHANNEL(Channel.VUW) CHLTYPE(SDR)
> 28 CONNAME(130.45.81.7(10206)) CURRENT
> 29 RQMNAME(THKF4ZF) STATUS(RUNNING)
> 30 SUBSTATE(MQGET) XMITQ(Channel.VUW.XMITQ)
>
> Want to create an awk script that could count the entries.
>
> Sample output.
>
> $ cat result.txt
> ChannelType Channel Conname Count
> SVRCONN Channel.ABC 123.45.123.67 2
> SVRCONN Channel.ABC 234.56.345.78 2
> RCVR Channel.XYZ 130.45.81.13 1
> SDR Channel.VUW 130.45.81.7(10206) 1
>
> Prefer to sort the output with count descending.
> Any help appreciated.

Here's an awk program that does what you want (though unsorted)...

awk '
/AMQ8417: Display Channel Status details\./ {
k1 = k2 = k3 = ""
next
}
match($1,/CHANNEL\(.*\)/) {
k2 = substr($1,RSTART+8,RLENGTH-9)
match($2,/\(.*\)/)
k1 = substr($2,RSTART+1,RLENGTH-2)
next
}
match($1,/CONNAME\(.*\)/) {
k3 = substr($1,RSTART+8,RLENGTH-9)
next
}
/SUBSTATE/ {
v = sprintf("%-12s %-12s %-19s", k1, k2, k3)
c[v]++
}
END {
printf("%-12s %-12s %-19s %s\n",
"ChannelType", "Channel", "Conname", "Count")
for (v in c)
printf("%s %d\n", v, c[v])
}
'

To sort it pipe the output (with appropriate options, e.g. -k4nr)
to Unix sort(1)...

awk '
...
' | sort -k4nr

or inspect GNU awk's sort functions if you have gawk available.

Janis

>
> TIA
>

From: Harry on
Janis Papanagnou wrote...

>Here's an awk program that does what you want (though unsorted)...
>
> awk '
> /AMQ8417: Display Channel Status details\./ {
> k1 = k2 = k3 = ""
> next
> }
> match($1,/CHANNEL\(.*\)/) {
> k2 = substr($1,RSTART+8,RLENGTH-9)
> match($2,/\(.*\)/)
> k1 = substr($2,RSTART+1,RLENGTH-2)
> next
> }
> match($1,/CONNAME\(.*\)/) {
> k3 = substr($1,RSTART+8,RLENGTH-9)
> next
> }
> /SUBSTATE/ {
> v = sprintf("%-12s %-12s %-19s", k1, k2, k3)
> c[v]++
> }
> END {
> printf("%-12s %-12s %-19s %s\n",
> "ChannelType", "Channel", "Conname", "Count")
> for (v in c)
> printf("%s %d\n", v, c[v])
> }
> '
>
>To sort it pipe the output (with appropriate options, e.g. -k4nr)
>to Unix sort(1)...
>
> awk '
> ...
> ' | sort -k4nr
>
>or inspect GNU awk's sort functions if you have gawk available.
>
>Janis


It works perfectly.

I have added extra codes so I can run it as a real time "connection
count monitor" every several minutes."

Thanks


From: Harry on
Janis Papanagnou wrote...
>
>On 17/07/10 06:03, Harry wrote:
>> Hi All
>>
>> I have an (much simplified) input file like this.
>>
>> $ cat -n connection.txt
>> 1 AMQ8417: Display Channel Status details.
>> 2 CHANNEL(Channel.ABC) CHLTYPE(SVRCONN)
>> 3 CONNAME(123.45.123.67) CURRENT
>> 4 RQMNAME( ) STATUS(RUNNING)
>> 5 SUBSTATE(RECEIVE) XMITQ( )
>> 6 AMQ8417: Display Channel Status details.
>> 7 CHANNEL(Channel.ABC) CHLTYPE(SVRCONN)
>> 8 CONNAME(123.45.123.67) CURRENT
>> 9 RQMNAME( ) STATUS(RUNNING)
>> 10 SUBSTATE(RECEIVE) XMITQ( )
>> 11 AMQ8417: Display Channel Status details.
>> 12 CHANNEL(Channel.ABC) CHLTYPE(SVRCONN)
>> 13 CONNAME(234.56.345.78) CURRENT
>> 14 RQMNAME( ) STATUS(RUNNING)
>> 15 SUBSTATE(RECEIVE) XMITQ( )
>> 16 AMQ8417: Display Channel Status details.
>> 17 CHANNEL(Channel.ABC) CHLTYPE(SVRCONN)
>> 18 CONNAME(234.56.345.78) CURRENT
>> 19 RQMNAME( ) STATUS(RUNNING)
>> 20 SUBSTATE(RECEIVE) XMITQ( )
>> 21 AMQ8417: Display Channel Status details.
>> 22 CHANNEL(Channel.XYZ) CHLTYPE(RCVR)
>> 23 CONNAME(130.45.81.13) CURRENT
>> 24 RQMNAME(THKF7A) STATUS(RUNNING)
>> 25 SUBSTATE(RECEIVE) XMITQ( )
>> 26 AMQ8417: Display Channel Status details.
>> 27 CHANNEL(Channel.VUW) CHLTYPE(SDR)
>> 28 CONNAME(130.45.81.7(10206)) CURRENT
>> 29 RQMNAME(THKF4ZF) STATUS(RUNNING)
>> 30 SUBSTATE(MQGET) XMITQ(Channel.VUW.XMITQ)
>>
>> Want to create an awk script that could count the entries.
>>
>> Sample output.
>>
>> $ cat result.txt
>> ChannelType Channel Conname Count
>> SVRCONN Channel.ABC 123.45.123.67 2
>> SVRCONN Channel.ABC 234.56.345.78 2
>> RCVR Channel.XYZ 130.45.81.13 1
>> SDR Channel.VUW 130.45.81.7(10206) 1
>>
>> Prefer to sort the output with count descending.
>> Any help appreciated.
>
>Here's an awk program that does what you want (though unsorted)...
>
> awk '
> /AMQ8417: Display Channel Status details\./ {
> k1 = k2 = k3 = ""
> next
> }
> match($1,/CHANNEL\(.*\)/) {
> k2 = substr($1,RSTART+8,RLENGTH-9)
> match($2,/\(.*\)/)
> k1 = substr($2,RSTART+1,RLENGTH-2)
> next
> }
> match($1,/CONNAME\(.*\)/) {
> k3 = substr($1,RSTART+8,RLENGTH-9)
> next
> }
> /SUBSTATE/ {
> v = sprintf("%-12s %-12s %-19s", k1, k2, k3)
> c[v]++
> }
> END {
> printf("%-12s %-12s %-19s %s\n",
> "ChannelType", "Channel", "Conname", "Count")
> for (v in c)
> printf("%s %d\n", v, c[v])
> }
> '
>
>Janis

I want to add the STATUS field as well.
But the RQMNAME field may be blank or non-blank.

I have added the following ... but it picks up STATUS onto k4
for the non-blank RQMNAME lines only.

match($1,/RQMNAME\(.*\)/) {
match($2,/\(.*\)/)
k4 = substr($2,RSTART+1,RLENGTH-2)
next
}

How could I pick up STATUS when RQMNAME is blank or non-blank ?

Thanks

From: Harry on
Harry wrote...

>I want to add the STATUS field as well.
>But the RQMNAME field may be blank or non-blank.
>
>I have added the following ... but it picks up STATUS onto k4
>for the non-blank RQMNAME lines only.
>
> match($1,/RQMNAME\(.*\)/) {
> match($2,/\(.*\)/)
> k4 = substr($2,RSTART+1,RLENGTH-2)
> next
> }
>
>How could I pick up STATUS when RQMNAME is blank or non-blank ?

Nevermind, just got it.

/STATUS/
match($0,/STATUS\(.*\)/) {
k4 = substr($0,RSTART+7,RLENGTH-8)
next
}