Help with parsing data with awk , eliminating unwanted data

Experts ,

Below is the data:

--- Physical volumes ---
PV Name /dev/dsk/c1t2d0
VG Name /dev/vg00
PV Status available
Allocatable yes
VGDA 2
Cur LV 8
PE Size (Mbytes) 8
Total PE 4350
Free PE 2036
Allocated PE 2314
Stale PE 0
IO Timeout (Seconds) default

--- Physical volumes ---
PV Name /dev/dsk/c1t2d1
VG Name /dev/vg01
PV Status available
Allocatable yes
VGDA 2
Cur LV 8
PE Size (Mbytes) 8
Total PE 43500
Free PE 20366
Allocated PE 2314
Stale PE 0
IO Timeout (Seconds) default

--- Physical volumes ---
PV Name /dev/dsk/c1t2d4
VG Name /dev/vg02
PV Status available
Allocatable yes
VGDA 2
Cur LV 8
PE Size (Mbytes) 8
Total PE 435044
Free PE 203645
Allocated PE 2314
Stale PE 0
IO Timeout (Seconds) default

--- Physical volumes ---
PV Name /dev/dsk/c1t2d5
VG Name /dev/vg03
PV Status available
Allocatable yes
VGDA 2
Cur LV 8
PE Size (Mbytes) 8
Total PE 435080
Free PE 2036890
Allocated PE 2314
Stale PE 0
IO Timeout (Seconds) default

I want the output as below: with "VG Name , PE Size ,Free PE" values to be printed side by side:

/dev/vg00 8 2036
/dev/vg01 8 20366
/dev/vg02 8 203645
/dev/vg03 8 2036890

Thank you,

 $ nawk '/VG Name/{v=$NF}/PE Size/{ps=$NF}/Free PE/{fp=$NF}fp{print v,ps,fp;fp="";}' input.txt
/dev/vg00 8 2036
/dev/vg01 8 20366
/dev/vg02 8 203645
/dev/vg03 8 2036890

if you dont have nawk, then use awk

1 Like

Since the output appears to have a strict format you could also try:

awk '{print $10, $24, $30}' RS= infile

But that would probably be more sensitive in case of possible future changes in the output of the command..

1 Like

itkamaraj ,

It works fine, thanks, However I am unable to understand

{fp=$NF}fp{print v,ps,fp;fp="";}'

part in the code , why you are using two times fp variable.
Appreciate if you could explain it.

Scrutinizer,
Nice , Thanks it works fine for the data . But If I have a data of huge length sa 1000 lines , the parsing with field number would be difficult in that way I think. Or is there any trick for a bigger data in the filed number wise.

Thanks,

Hi reveri,

Setting RS to the empty string is a special condition where records are split if there are two consecutive newlines. There would not be a limit on the number of records.

There would be a limitation per record, if the record exceeds the maximum length, which would not happen very quickly, but in the awk version of HP-UX (that is what I think this it, right? ) sooner than on some other platforms..

--
{fp=$NF} belongs to the preceding condition-action pair.

fp{print v,ps,fp;fp="";}' means if fp is neither empty nor zero then process {print v,ps,fp;fp="";}

--
Here is another version without RS=

awk '/VG Name/{a=$NF} /PE Size/{b=$NF} /Free PE/{print a,b,$NF}' infile

or

awk '/VG Name|PE Size|Free PE/{printf "%s ", $NF} /Free PE/{print x}' infile
1 Like

Scrutinizer,
Thanks, yes it was from hp-ux ,

Thanks for the time, detail and explaining,
Reveri.