1748129 Members
3679 Online
108758 Solutions
New Discussion юеВ

Re: Match fields in awk

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

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

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...
Hein van den Heuvel
Honored Contributor

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





Dennis Handly
Acclaimed Contributor

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?
lawrenzo_1
Super Advisor

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

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...
lawrenzo_1
Super Advisor

Re: Match fields in awk

Thanks all,

thanks James - just what I was looking for.

Chris
hello