Re: Match fields in awk (209 Views)
Reply
Super Advisor
lawrenzo_1
Posts: 560
Registered: ‎06-06-2003
Message 1 of 7 (209 Views)
Accepted Solution

Match fields in awk

I was hoping someone could help with my understanding of awk matching fields:

I can print each field from a file:

awk -F ',' '{for (i = 1; i < NF ; i++) {print $i}}' lparcpu.out

lpar_name=juniper
lpar_id=18
curr_shared_proc_pool_id=0
curr_proc_mode=shared
curr_min_proc_units=1.0
curr_proc_units=2.0
curr_max_proc_units=4.0
curr_min_procs=2
curr_procs=2
curr_max_procs=4
curr_sharing_mode=cap
curr_uncap_weight=0
pend_shared_proc_pool_id=0
pend_proc_mode=shared
pend_min_proc_units=1.0
pend_proc_units=2.0
pend_max_proc_units=4.0
pend_min_procs=2
pend_procs=2
pend_max_procs=4
pend_sharing_mode=cap
pend_uncap_weight=0
run_proc_units=0.0
run_procs=0
' cpu.out

how can I then search a field and print on one line:

awk -F ',' '
{for (i = 1; i < NF ; i++)
$i ~ /lpar_name/ { NAME = $0 }
$i ~ /curr_min_proc/ { MIN = $0 }
print NAME,MIN}
' lparcpu.out

my understanding is incorrect.

any help much appreciated

Thanks

Chris
hello
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 7 (209 Views)

Re: Match fields in awk

Hi Chreis:

I believe you mean (want):

awk -F ',' '
/lpar_name/ { NAME = $0 }
/curr_min_proc/ { MIN = $0 }
END{print NAME,MIN}
' lparcpu.out

Regards!

...JRF...
Please use plain text.
Honored Contributor
Hein van den Heuvel
Posts: 6,585
Registered: ‎05-19-2003
Message 3 of 7 (209 Views)

Re: Match fields in awk

Well, if I understand the problem correctly you could for example double-barrel awk. First stage splits into lines, second stages looks for desired words, remembering as it goes, and printing when last match is seen or an end marker is seen.
Note: It is 'nice' to clear all variables after print to prevent a prior match to re-appear.

$ awk -F ',' '{for (i = 1; i < NF ; i++) {print $i}}' lparcpu.out | awk -F '=' '/lpar_name/{n=$2} /curr_
min_procs/{ print n,$2}'
juniper 2

Or... just tell awk to treat the comma as record separator. However, now you have to 'split' the lines to find the values.

$ awk -v RS=',' '/lpar_name/{split($0,a,"="); n=a[2]} /curr_min_procs/{split($0,a,"="); print n,a[2
]}' lparcpu.out
juniper 2

in PERL you woudl just match for 'chunks' and remember a part of the chunk as you go:

$ perl -ne '$name=$1 if /lpar_name=(\w+)/; print qq($name\t$1\n) if /curr_min_procs=(\d+)/' lparcpu.out
juniper 2

hth,
Hein





Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,977
Registered: ‎03-06-2006
Message 4 of 7 (209 Views)

Re: Match fields in awk

for (i = 1; i < NF ; i++)
$i ~ /lpar_name/ { NAME = $0 }
$i ~ /curr_min_proc/ { MIN = $0 }
print NAME,MIN}

Not sure what you want here?
It seems you forgot the {} around both matches?
Did you really want to print the same line twice?
Please use plain text.
Super Advisor
lawrenzo_1
Posts: 560
Registered: ‎06-06-2003
Message 5 of 7 (209 Views)

Re: Match fields in awk

I guess I didnt explain what I was looking for, I want the output to appear as:

I should mention each line in the file I am analysing appears as

lpar_name=juniper,lpar_id=2,curr_shared_proc_pool_id=0,curr_proc_mode=shared,curr_min_proc_units=0.25,curr_proc_units=0.5,curr_max_proc_units=4.0,curr_min_procs=1,curr_procs=1,curr_max_procs=4,curr_sharing_mode=uncap,curr_uncap_weight=128,pend_shared_proc_pool_id=0,pend_proc_mode=shared,pend_min_proc_units=0.25,pend_proc_units=0.5,pend_max_proc_units=4.0,pend_min_procs=1,pend_procs=1,pend_max_procs=4,pend_sharing_mode=uncap,pend_uncap_weight=128,run_proc_units=0.5,run_procs=1,run_uncap_weight=128

I want to extract certain fields with one command rather than running several - the current command I run is

awk -F ',' '/'"$lpardata"'/ {split($5,a,"=");print a[2]}' lpardata=$lpardata ${CONFIG_DIR}/lparcpu.out

thus giving me the value of $6 however I want to analyse the whole line and extract data for several parameters - in this example I want the output to be

juniper 1

i dont mind if the output was

lpar_name=juniper curr_min_proc_units=1.0

hope that makes more sense

Thanks

Chris






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

Re: Match fields in awk

Hi (again) Chris:

> I should mention each line in the file I am analysing appears as [ one big line, and ] I want to extract certain fields with one command rather than running several - the current command I run is

> awk -F ',' '/'"$lpardata"'/ {split($5,a,"=");print a[2]}' lpardata=$lpardata ${CONFIG_DIR}/lparcpu.out

> I want to analyse the whole line and extract data for several parameters - in this example I want the output to be

> juniper 1

Fine, but I don't see how you get "juniper 1".

See if this is closer to what you want:

# awk -F "," '{for (n=1;n
Regards!

...JRF...
Please use plain text.
Super Advisor
lawrenzo_1
Posts: 560
Registered: ‎06-06-2003
Message 7 of 7 (209 Views)

Re: Match fields in awk

Thanks all,

thanks James - just what I was looking for.

Chris
hello
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