- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Match fields in awk
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-08-2011 02:20 AM
тАО04-08-2011 02:20 AM
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
Solved! Go to Solution.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-08-2011 02:43 AM
тАО04-08-2011 02:43 AM
Re: Match fields in awk
I believe you mean (want):
awk -F ',' '
/lpar_name/ { NAME = $0 }
/curr_min_proc/ { MIN = $0 }
END{print NAME,MIN}
' lparcpu.out
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-08-2011 05:54 AM
тАО04-08-2011 05:54 AM
Re: Match fields in awk
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-10-2011 11:37 AM
тАО04-10-2011 11:37 AM
Re: Match fields in awk
$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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-11-2011 06:19 AM
тАО04-11-2011 06:19 AM
Re: Match fields in awk
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-11-2011 06:59 AM
тАО04-11-2011 06:59 AM
Solution> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-11-2011 07:03 AM
тАО04-11-2011 07:03 AM
Re: Match fields in awk
thanks James - just what I was looking for.
Chris