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 can I grep a "previous" string?

Status
Not open for further replies.

urs

Programmer
Oct 19, 1999
1
0
0
CH
Hello<br><br>I've got a log-file like this: <br><br>Tue Aug 15 00:28:12 2000<br>Creating Lookup Cache<br>Tue Aug 15 00:30:45 2000<br>Lookup cache creation completed<br>Tue Aug 15 00:30:45 2000<br>Creating Lookup Cache<br>Tue Aug 15 00:30:45 2000<br>Lookup cache creation completed<br><br>And I need to get the time of the latest Look up cache. Therfore I tried to search the last&nbsp;&nbsp;string like &quot;Lookup cache&quot; minus 1 line and give the timestamp out. How would you do that?<br><br>Thank very mouch for any hints, Urs<br> <p>Urs Grubenmann<br><a href=mailto:urs.grubenmann@switzerland.org>urs.grubenmann@switzerland.org</a><br><a href= > </a><br>
 
a short perl script<br><br><FONT FACE=monospace><b><br>#!/usr/bin/perl -w<br>my $prev_line;<br><br>while(&lt;&gt;){<br>&nbsp;&nbsp;&nbsp;&nbsp;chomp;<br>&nbsp;&nbsp;&nbsp;&nbsp;if(/Lookup cache creation completed/){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;$_ $prev_line\n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;$prev_line = $_;<br>}<br></font></b><br><br>how to use it:<br><br>put the above into a file, call it prev_match.pl maybe<br><br>make sure the first line of prev_match.pl, the #! line, matches the location of perl on your system,<br><br>chmod +x prev_match.pl<br><br>use prev_match.pl like this<br><br><FONT FACE=monospace><b>prev_match.pl log-file</font></b><br><br>Shout up if you don't have Perl and someone will start you off on an awk script, or something. <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
tail -1 log-file ¦ head -1 <p>Ged Jones<br><a href=mailto:gedejones@hotmail.com>gedejones@hotmail.com</a><br><a href= > </a><br>
 
tail -2 log-file ¦ head -1<br><br>Sorry about the obvious error in the previous. <p>Ged Jones<br><a href=mailto:gedejones@hotmail.com>gedejones@hotmail.com</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top