Operating System - HP-UX
1847664 Members
3616 Online
110265 Solutions
New Discussion

Awk - using one field separator, then another?

 
SOLVED
Go to solution
Tony Walker
Frequent Advisor

Awk - using one field separator, then another?

Hi guys,

Here's one thats been bugging me for a while. Basically, I want to pull out the current load average from uptime ie :
10:46am up 55 days, 14:06, 159 users, load average: 3.34, 3.28, 3.39

Theory goes, use and FS of ":" and get the 4th field, then use an FS of "," and get the first thus ending up with 3.34. I'm sure there are better ways but I've come across a similar problem before and want to know whether this is possible in awk?

As always, help greatly appreciated.

Cheers,

Tony
11 REPLIES 11
Mark Grant
Honored Contributor

Re: Awk - using one field separator, then another?

This depends a little bit on how you want the information returned toyou but how about

expr "`uptime`" : ".*load average: \(.*\)" and pipe the
output into "cut" or "awk" or whatever.
Never preceed any demonstration with anything more predictive than "watch this"
Jean-Luc Oudart
Honored Contributor

Re: Awk - using one field separator, then another?

Tony

1) you can replace "," with spaces either with sed or tr before you pipe results into awk

2) you can use the split function in awk
split(s,a,fs) fs = field separator

Rgds,
Jean-Luc
fiat lux
Tony Walker
Frequent Advisor

Re: Awk - using one field separator, then another?

Jean-luc - I should have awarded you 10points! Just tried the split function in awk and it works just how I want.

Thanks!

Tony
Jean-Luc Oudart
Honored Contributor
Solution

Re: Awk - using one field separator, then another?

Usually the "bunny" means that solution is provided and complete.

Rgds,
Jean-Luc
fiat lux
Rob_132
Regular Advisor

Re: Awk - using one field separator, then another?

Zero points

It looks like you Jean-Luc, and you now have the opportunity to award them. At any rate, another approach could be:

echo $In|cut -d\: -f2|cut -d\, -f[1|2|3]

choose which field [1|2|3] you want....
Not looking for points - just providing an alternative.

Rob

Zero points
Rob_132
Regular Advisor

Re: Awk - using one field separator, then another?

Zero points

make that,

"It looks like you OWE Jean-Luc...."

Rob

Zero points
Rob_132
Regular Advisor

Re: Awk - using one field separator, then another?

Zero points

(sorry!!)...AND *Y O U* will be the one to promote Jean-Luc to *W I Z A R D*

Too compelling to pass up, I'd say!

Rob

Zero points
Tony Walker
Frequent Advisor

Re: Awk - using one field separator, then another?

Thanks for the input Rob, I'll give you some points you didn't ask for later:) So, you the points system then...
Steven E. Protter
Exalted Contributor

Re: Awk - using one field separator, then another?

Come on, make Jean-Luc a Wizard.

I would like to see a complete example.

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
Tony Walker
Frequent Advisor

Re: Awk - using one field separator, then another?

Steve - just for you..

uptime|awk -F: '{MYVAR=$4;split(MYVAR,mv,",");print mv[1]}'
Hein van den Heuvel
Honored Contributor

Re: Awk - using one field separator, then another?


The -F argument and corresponding FS variable is a full regular expression. So in this case you can just use both "," and ":" as seperators giving:

uptime|awk -F[:,] '{print $7}'

fwiw,
Hein.