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!

AWK matching - Why am I making this more difficult 1

Status
Not open for further replies.

AnotherAlan

Technical User
Feb 10, 2006
362
GB
Should be simple, but haven't used awk in a while and am currently snowbound in the uk, so trying to do this from a cygwin emulation on a dodgy laptop; however, i'm sure i'm making it more difficult than it needs be.

Problem: Retrieve data from log file based on two parameters;The order and matching pattern.
So, in the below, the order 1290119786 will give me the pattern to search on [103].

[2010-11-19 15:42:40.313 INFO] Logfile [103] Creating update order 1290119786

I can do this with a couple of greps, but was attempting something with awk because I thought it would be faster; as the log files can be huge.

num=`grep <order number> logfile | awk '{print $5}'| uniq`
awk -v num=$num '$5=="["num"]" {print $0}' testfile.log

this gets me close, although I would still need to strip out the [ and ]'s from the original grep to get the num variable. My real issue therefore is to get awk to accept [103] as a string comparison in $5, but all iterations i've tried just don't seem to work. Now i'm not sure if this is a cygwin quoting interpretation or just my rusty old awk / brain.

All help appreciated.
Thanks
 
Hi

You forgot to post log lines which should be found by the second step, when searching for 103. For now I would go with something like this :
Code:
awk 'FNR==NR{if($NF=="1290119786")p=$5;next}$5==p' /input/file /input/file

Feherke.
 
What are logfile and testfile.log ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi Feherke and PHV,

Thanks for the speedy replies and sorry for the confusion.

Essentially every order, e.g. 1290119786, has a corresponding pattern, e.g. [103], and this is the common factor for all info related to the order. i.e. the order number is only issued once within the log, and all subsequent entries to the logfile are based on the pattern.
So I search for the given order, retrieve the pattern and then search the same log for the pattern rather than the order.

logfile and testfile.log in the code example are just subsets of the same log I've been testing on.

Hope this makes sense and thanks for the assistance.
Regards
 
Marvellous, that is an inspired piece of awkness.
Thank you very much for your help.

Alan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top