Operating System - HP-UX
1745827 Members
4116 Online
108722 Solutions
New Discussion

awk help - printing fields in a record

 
lawrenzo
Trusted Contributor

awk help - printing fields in a record

Good afternoon

 

I am attempting to print certain fields from a file using awk but really am struggling with the output ....

 

I execute a home grown script

 

pclroute -c sl_hosts "lsvg rootvg"

 

the output is:

 

[sl37]
VOLUME GROUP:       rootvg                   VG IDENTIFIER:  00c8d37e00004c00000001378f20f6fe
VG STATE:           active                   PP SIZE:        32 megabyte(s)
VG PERMISSION:      read/write               TOTAL PPs:      1022 (32704 megabytes)
MAX LVs:            256                      FREE PPs:       155 (4960 megabytes)
LVs:                20                       USED PPs:       867 (27744 megabytes)
OPEN LVs:           19                       QUORUM:         2 (Enabled)
TOTAL PVs:          2                        VG DESCRIPTORS: 3
STALE PVs:          0                        STALE PPs:      0
ACTIVE PVs:         2                        AUTO ON:        yes
MAX PPs per VG:     32512
MAX PPs per PV:     1016                     MAX PVs:        32
LTG size (Dynamic): 256 kilobyte(s)          AUTO SYNC:      no
HOT SPARE:          no                       BB POLICY:      relocatable
PV RESTRICTION:     none                     INFINITE RETRY: no

[sl39]
VOLUME GROUP:       rootvg                   VG IDENTIFIER:  00c8d37e00004c0000000129f5b5f02c
VG STATE:           active                   PP SIZE:        32 megabyte(s)
VG PERMISSION:      read/write               TOTAL PPs:      1022 (32704 megabytes)
MAX LVs:            256                      FREE PPs:       516 (16512 megabytes)
LVs:                17                       USED PPs:       506 (16192 megabytes)
OPEN LVs:           16                       QUORUM:         2 (Enabled)
TOTAL PVs:          2                        VG DESCRIPTORS: 3
STALE PVs:          0                        STALE PPs:      0
ACTIVE PVs:         2                        AUTO ON:        yes
MAX PPs per VG:     32512
MAX PPs per PV:     1016                     MAX PVs:        32
LTG size (Dynamic): 256 kilobyte(s)          AUTO SYNC:      no
HOT SPARE:          no                       BB POLICY:      relocatable
PV RESTRICTION:     none                     INFINITE RETRY: no

[sl41]
VOLUME GROUP:       rootvg                   VG IDENTIFIER:  00c8d37e00004c00000001125668753e
VG STATE:           active                   PP SIZE:        32 megabyte(s)
VG PERMISSION:      read/write               TOTAL PPs:      1022 (32704 megabytes)
MAX LVs:            256                      FREE PPs:       454 (14528 megabytes)
LVs:                18                       USED PPs:       568 (18176 megabytes)
OPEN LVs:           17                       QUORUM:         2 (Enabled)
TOTAL PVs:          2                        VG DESCRIPTORS: 3
STALE PVs:          0                        STALE PPs:      0
ACTIVE PVs:         2                        AUTO ON:        yes
MAX PPs per VG:     32512
MAX PPs per PV:     1016                     MAX PVs:        32
LTG size (Dynamic): 256 kilobyte(s)          AUTO SYNC:      no
HOT SPARE:          no                       BB POLICY:      relocatable
PV RESTRICTION:     none                     INFINITE RETRY: no

so I execute the same script with the following awk syntax but it doesnt quite work as I expect it too:

 

pclroute -c sl_hosts lsvg rootvg |awk 'BEGIN{H=0;PP=0;TP=0}
        /^\[/ { H = $0}
        /PP SIZE/ { PP = $6 }
        /TOTAL PP/ { TP = $6 }
        {print H,PP,TP}'

[sl37] 32 1022
[sl37] 32 1022
[sl37] 32 1022
[sl37] 32 1022
[sl37] 32 1022
[sl37] 32 1022
[sl37] 32 1022
[sl37] 32 1022
[sl37] 32 1022
[sl37] 32 1022
[sl37] 32 1022
[sl37] 32 1022
[sl37] 32 1022
[sl37] 32 1022
[sl37] 32 1022
[sl37] 32 1022
[sl39] 32 1022
[sl39] 32 1022
[sl39] 32 1022
[sl39] 32 1022
[sl39] 32 1022
[sl39] 32 1022
[sl39] 32 1022
[sl39] 32 1022
[sl39] 32 1022
[sl39] 32 1022
[sl39] 32 1022
[sl39] 32 1022
[sl39] 32 1022
[sl39] 32 1022
[sl39] 32 1022
[sl39] 32 1022
[sl41] 32 1022
[sl41] 32 1022
[sl41] 32 1022
[sl41] 32 1022
[sl41] 32 1022
[sl41] 32 1022
[sl41] 32 1022
[sl41] 32 1022
[sl41] 32 1022
[sl41] 32 1022
[sl41] 32 1022
[sl41] 32 1022
[sl41] 32 1022
[sl41] 32 1022
[sl41] 32 1022
[sl41] 32 1022
[sl41] 32 1022
[sl41] 32 1022
[sl41] 32 1022

as always any help or suggestions would be greatly appreciated

 

thanks

 

Chris

hello
4 REPLIES 4
lawrenzo
Trusted Contributor

Re: awk help - printing fields in a record

hmmmm maybe if I remembered help I got in the past I'd be able to do this ....

 

I have searched my notes and previous posts and found this:

 

pclroute -c sl_hosts lsvg rootvg |awk '/^\[/ {if (line != "") print line; line =$NF}  /PP SIZE/{line=line ":" $6} /TOTAL PP/ {line=line ":" $6} END {print line}'

 

[sl37]:32:1022
[sl39]:32:1022
[sl41]:32:1022

open to other suggestions though :)

 

Chris

hello
Paul Haygarth
Advisor

Re: awk help - printing fields in a record

This should work too ;)

 

pclroute -c sl_hosts lsvg rootvg | awk '/^\[/ {printf "%s ", $0} /PP SIZE/ {printf "%s ", $6}; /TOTAL PP/

{printf "%s\n", $6}'

Dennis Handly
Acclaimed Contributor

Re: awk help - printing fields in a record

>This should work too

 

Probably better to accumulate the data in a variable then output all at once (and format it like a program ;-):

pclroute -c sl_hosts lsvg rootvg | awk '

/^\[/ {

   line = $0

}

/PP SIZE/ {

   line = line ":"  $6

}

/TOTAL PP/ {
   print line ":" $6

}'

 

Of course bad things will happen if there isn't that [x], PP SIZE and TOTAL PP sequence.

lawrenzo
Trusted Contributor

Re: awk help - printing fields in a record

thank you gents

 

Chris

 

hello