Operating System - Linux
1752614 Members
4196 Online
108788 Solutions
New Discussion юеВ

Re: print fields suing awk

 
SOLVED
Go to solution
lawrenzo
Trusted Contributor

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
12 REPLIES 12
lawrenzo
Trusted Contributor

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
James R. Ferguson
Acclaimed Contributor

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...
Steven E. Protter
Exalted Contributor

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
Wouter Jagers
Honored Contributor
Solution

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.
Jeff_Traigle
Honored Contributor

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
James R. Ferguson
Acclaimed Contributor

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...
Peter Godron
Honored Contributor

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)}'
lawrenzo
Trusted Contributor

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
James R. Ferguson
Acclaimed Contributor

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...