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

How do I search a file for a string and report findings.

Status
Not open for further replies.

bosoxer1

Programmer
Jan 22, 2005
20
0
0
US
I have log file that contains job names with start and end times. I have to search through the log to find "jobname started" and hold it, then search again for "jobname ended", and then report the jobname, start time, end time, elapsed time.

I have 12 jobs to search for and report on, so that may alter the way to track this down. Any thoughts on how to write a sophisticated search and report results?

example of log:
Fri Jul 08 13:44:55 EDT 2005 ......jobname started.
...
...
Fri Jul 08 13:49:45 EDT 2005 ......jobname ended.


Thanks.
 
nawk -f bosox.awk myLog.log

bosox.awk:
Code:
/started.$/{ job=$(NF-1); arrS[job]=$1 OFS $2 OFS $3 OFS $4 OFS $5 OFS $6}
/ended.$/{ job=$(NF-1); ;arrE[job]=$1 OFS $2 OFS $3 OFS $4 OFS $5 OFS $6}
END {
  for( job in arrS )
    printf(" job->[%s]: start->[%s] end->[%s]\n", job, arrS[job], arrE[job])
}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hi

May I suggest some little modification ? bosoxer1 said something about elapsed time too. In this case I prefer to keep the convert the timestamp to Unix time.
Code:
BEGIN {
  split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",month)
  for (m in month) month[month[m]]=m
}

/started.$/ { job=$(NF-1); arrS[job]=mktime(gensub(/:/," ","g",$6 " " month[$2] " " $3 " " $4)) }
/ended.$/ { job=$(NF-1); arrE[job]=mktime(gensub(/:/," ","g",$6 " " month[$2] " " $3 " " $4)) }

END {
  for (job in arrS)
    printf "job->[%s]: start->[%s] end->[%s] dif->[%d]\n", job,strftime("%c",arrS[job]),strftime("%c",arrE[job]),arrE[job]-arrS[job]
}

The above is working with [tt]qawk[/tt]. I hope that "[tt][%s][/tt]" has no special meaning in [tt]nawk[/tt].

Feherke.
 
Thanks for the great method to break down the file.

I still need to be able to search for a string though. I need to find the string of job "ABC Started" somewhere on the line, then capture the date and time, then search for another string on another row of the file, and search for "ABC Ended", and capture date and time.

I will then have to calculate elapsed time by comparing the two, and sending all captured information to a seperate, comma delimited file.

Thanks...Dave...
 
.... and waht exactly in the feherke's solution doesn't work?

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top