print fields suing awk (183 Views)
Reply
Trusted Contributor
lawrenzo
Posts: 383
Registered: ‎06-06-2003
Message 1 of 13 (183 Views)
Accepted Solution

print fields suing awk

Hi guys,

I posted a thread earlier but I think I need to elaborate, I am still learning awk and my syntax is all over the place ie prints unexpected fields etc ....

eg I am running these commands on AIX so I hope that doesnt offend the HPUX guru's here ...

eg:

lsvg rootvg

VOLUME GROUP: rootvg VG IDENTIFIER: 0045278a00004c000000010deef5d831
VG STATE: active PP SIZE: 128 megabyte(s)
VG PERMISSION: read/write TOTAL PPs: 270 (34560 megabytes)
MAX LVs: 256 FREE PPs: 72 (9216 megabytes)
LVs: 11 USED PPs: 198 (25344 megabytes)
OPEN LVs: 10 QUORUM: 1
TOTAL PVs: 2 VG DESCRIPTORS: 3
STALE PVs: 0 STALE PPs: 0
ACTIVE PVs: 2 AUTO ON: no
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


from this output I want to print

TOTAL 270 34560
FREE 72 9216
USED 198 25344

the problem I am having is determining the field and rows to print in awk ....

any help is much appreciated.

Thanks

Chris
hello
Please use plain text.
Trusted Contributor
lawrenzo
Posts: 383
Registered: ‎06-06-2003
Message 2 of 13 (183 Views)

Re: print fields suing awk

i should elaborate as the example text is not very clear ...

from the lsvg rootvg the actual fields are:

TOTAL PPs: 270 (34560 megabytes)
FREE PPs: 72 (9216 megabytes)
USED PPs: 198 (25344 megabytes)

lsvg rootvg |awk '/PPs:/ && ! /STALE/ {print ????}'

???? is the problem because the USED field is not $4,$6,$7

Thanks

hello
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 13 (183 Views)

Re: print fields suing awk

Hi Chris:

Here's one way. You can adapt the matching for the FREE and USED portions easily:

# lsvg rootvg|awk '/TOTAL PP/ {split($7,a,/[()]/);print $4,$6,a[2]}'

Regards!

...JRF...
Please use plain text.
Exalted Contributor
Steven E. Protter
Posts: 33,806
Registered: ‎08-15-2002
Message 4 of 13 (183 Views)

Re: print fields suing awk

Chris,

When you say you had an earlier thread, post a link.

I think this is it.

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1121029

I can discern little else from your input, but will give you a little example.

Lets say I have output that looks like this:

Field1 Field2 Field3

To print it.

cat (whatever) | awk '{ print $1 $2 $3}'

To get field two into a variable

F2=$(cat $DATA | awk '{ print $2 }')

Try and take some time to explain the problem and give background.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Please use plain text.
Honored Contributor
Wouter Jagers
Posts: 495
Registered: ‎10-01-2002
Message 5 of 13 (183 Views)

Re: print fields suing awk

Hiya,

Usually I call sed to the rescue in such situations (sed & awk get along great).

Try this:

# lsvg rootvg | sed -ne 's/[()]//g' -e 's/.*\(TOTAL PP.*\)/\1/p' -e 's/.*\(FREE PP.*\)/\1/p' -e 's/.*\(USED PP.*\)/\1/p'

After that, the awk becomes way easier ;-)

|awk '{print $1,$3,$4}'


Cheers,
Wout
an engineer's aim in a discussion is not to persuade, but to clarify.
Please use plain text.
Honored Contributor
Jeff_Traigle
Posts: 1,354
Registered: ‎03-04-2004
Message 6 of 13 (183 Views)

Re: print fields suing awk

I think you're wanting something like this since the line with USED isn't the same number of fields as the other two:

lsvg rootvg | awk '/PPs:/ && ! /STALE/ {if ($1 == "LVs:") print $3, $5, $6 else print $4, $6, $7}' | sed 's/(//'

The sed is just to get rid of that ( in the third printed field.
--
Jeff Traigle
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 7 of 13 (183 Views)

Re: print fields suing awk

Hi (again) Chris:

Since the "USED" fields are not ordinally in the same place as the "TOTAL" or "FREE" lines, we can do this:

# lsvg rootvg | awk '/TOTAL PP|FREE PP/ {split($7,a,/[()]/);print $4,$6,a[2]};/USED PP/ {split($6,a,/[()]/);print $3,$5,a[2]}'

TOTAL 270 34560
FREE 72 9216
USED 198 25344

Regards!

...JRF...
Please use plain text.
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 8 of 13 (183 Views)

Re: print fields suing awk

Chris,
my sad effort would be along the lines of:
awk '{print $(NF-4),$(NF-3),$(NF-2),$(NF-1)}'
Please use plain text.
Trusted Contributor
lawrenzo
Posts: 383
Registered: ‎06-06-2003
Message 9 of 13 (183 Views)

Re: print fields suing awk

thread reopened because the output was not correct.

Brief explaination ...

we upload stats to a central DB which then give us peformance and capacity data, this data will tell us when we will need to add more disk to the system.

The capacity manager wants the output to appear as below without the headers because he will set this at his end:

volumegroup,PP SIZE,FREE PPs,TOTAL PPs

rootvg,128,72,270

Thanks again
hello
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 10 of 13 (183 Views)

Re: print fields suing awk

Hi Chris:

# lsvg rootvg | awk 'NR==1 {VG=$3};/PP SIZE/ {SZ=$6};/FREE PP/ {FREE=$6};/TOTAL PP/ {TOT=$6};END{OFS=",";print VG,SZ,FREE,TOT}'

rootvg,128,72,270

Regards!

...JRF...
Please use plain text.
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 11 of 13 (183 Views)

Re: print fields suing awk

Try the awk script below. It basically extends JRF's script if there is more than one VG present:

# awk '/VOLUME GROUP/{if(a) printf("%s ",a);a=$3}
/PP SIZE/{if(b) printf("%s ",b);b=$6}
/TOTAL PPs/{if(c) printf("%s ",c);c=$6}
/FREE PPs/{if(d) printf("%s\n",d);d=$6}
END{print a,b,c,d}' inp
Please use plain text.
Trusted Contributor
lawrenzo
Posts: 383
Registered: ‎06-06-2003
Message 12 of 13 (183 Views)

Re: print fields suing awk

Thanks guys,

both examples are good however sandman I will toruble shoot your example and get back to you as no output is displayed.

hello
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,744
Registered: ‎03-06-2006
Message 13 of 13 (183 Views)

Re: print fields using awk

[ Edited ]

>I will trouble shoot your example and get back to you as no output is displayed.

Hmm, I get output if you assume that you made two copies of your initial lsvg output. Sandman does have the free after the total.

Here my suggested changes that prints complete lines:
awk '
BEGIN { VG=""; SZ=""; FREE=""; TOT="" }
/VOLUME GROUP/ {
   if (VG) print VG "," SZ "," FREE "," TOT
   VG=$3
}
/PP SIZE/ {SZ=$6}
/TOTAL PPs/ {TOT=$6}
/FREE PPs/ {FREE=$6}
END { print VG "," SZ "," FREE "," TOT }'

The BEGIN isn't strictly needed.
You could use OFS like JRF did in his print.

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation