Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk '{print $}' calculation with variable? 1

Status
Not open for further replies.

sillyVM

Technical User
Feb 14, 2007
144
US
hey all linux gurus, I have a little technical difficulties.

here is what i have
[linux]$ ps aux | grep resin-3.0.19/log | grep -v grep | awk '{ print $6 }'
2262812
2214548
[linux]$ ps aux | grep resin-3.0.19/log | grep -v grep | head -1 | awk '{ print $6 }'
2262812
[linux]$ ps aux | grep resin-3.0.19/log | grep -v grep | tail -1 | awk '{ print $6 }'
2214548
How do I print a value with head+tail? I want the value of 2262812+2214548.
Is there a way I can print that out in one command line? Say print $6.row1+row2?
 
Try this (untested):

[tt]ps aux | awk '
/resin-3.0.19\/log/ && lastfound="" { firstfound=$6 ; next }
/resin-3.0.19\/log/ { lastfound=$6 }
END { print firstfound+lastfound }
'[/tt]

Annihilannic.
 
thanks Annihilannic,
although i didn't quite comprehend the code, but the variable firstfound did not return any value. it's empty. I am not really sure how to do this, but the last found part did return the right value. and it got the second value i wanted.
 
Sorry, a typo and a logic error... try this:

Code:
awk '
        # if line matches and firstfound value is not yet defined
        # sets the value of firstfound and skips to next line
        /resin-3.0.19\/log/ && [COLOR=red]first[/color]found[COLOR=red]=[/color]="" { firstfound=$6 ; next }
        # if line matches, set the value of lastfound, overwriting
        # any previously set value
        /resin-3.0.19\/log/ { lastfound=$6 }
        # when all input is processed print the sum of the two
        END { print firstfound+lastfound }
'

Annihilannic.
 
Genius! it worked. I see, added to my notes. Thanks for the comments & explainations. It's very clear now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top