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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieve data from non static columns 2

Status
Not open for further replies.

AnotherAlan

Technical User
Feb 10, 2006
362
GB
Hi All,

Another AWK problem beyond my current skillset.

I have a logfile which I need to retrieve a specific number value from. Unfortunately there is no static column in the log to pull this data from. The only constant is that the value is always in the field after the word "took". I have managed to pull all the relevant data to parse from the log but am now stuck at how to get NF+1 to work.


Example:
2010-12-14 01:42:06,910 [Dispatcher-4] INFO B2B - BEFORE login 729614 for 183445953
2010-12-14 01:42:07,274 [Dispatcher-4] INFO B2B - AFTER login took 364 ms for 183445953
2010-12-14 01:42:07,807 [Dispatcher-4] INFO B2B - BEFORE priceQuote site=20006USDJPY for 183445953
2010-12-14 01:42:08,917 [Dispatcher-4] INFO B2B - AFTER priceQuote site=20006 USDJPY took 1110 ms for 183445953
2010-12-14 01:42:08,918 [Dispatcher-4] INFO B2B - requestQuote : BEFORE call to pricing service
2010-12-14 01:42:09,741 [Dispatcher-4] INFO B2B - requestQuote : AFTER call to pricing service took 823 ms

In this instance I would like to pull 364,1110 and 823.

All help appreciated as always.
Thanks

 
Hi

I would not do it by fields :
Code:
awk '/ took /{print gensub(/.* took ([^ ]+).*/,"\\1","")}' /input/file
Code:
awk '/ took /{sub(/.* took /,"");sub(/ .*/,"");print}' /input/file
Code:
sed -n 's/.* took \(\S\+\).*/\1/p' /input/file
Tested with [tt]gawk[/tt], [tt]mawk[/tt] and GNU [tt]sed[/tt].

Feherke.
 
Hi

Anyway, here is how you can get it by fields :
Code:
[gray]# minimal[/gray]
awk '{for(i=1;i<NF;i++)if($i=="took")print$(i+1)}' /input/file

[gray]# optimized[/gray]
awk '/ took /{for(i=1;i<NF;i++)if($i=="took"){print$(i+1);next}}' /input/file
Tested with [tt]gawk[/tt] and [tt]mawk[/tt].

Feherke.
 
Once again you come up with the goods Feherke. Thank you so much.

Regards
Alan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top