Solved

Need to create shell script that finds a phrase and then prints the line right above that phrase to another file

Posted on 2003-11-26
8
233 Views
Last Modified: 2010-04-17
I need to create a shell script (or perl) that will search a given logfile for a particular phrase and then copy the date and time of which is just above the occurance of the phrase.

Example:

11/26/2003;10:31:05
Verification Startup is completed
Welcome to the Dialogue
11/26/2003;10:32:11 Transition started
11/26/2003;10:32:14 Manipulation error shutdown completed
11/26/2003;10:41:25
Verification Startup is completed
Welcome to the Dialogue

Given the logfile above I need to write another file that will contain this:

Startup times
11/26/2003;10:31:05
11/26/2003;10:41:25

Please help, thanks. I am writing this in an HP9000 machine. Unix.

0
Comment
Question by:jporgy
  • 3
8 Comments
 

Author Comment

by:jporgy
ID: 9829519
This is a unix perl or shell script programming
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9829597
echo Startup times > output_file_name
grep "string_to_search" log_file_name | awk ' { print $1 " " $2 } ' >> output_file_name
0
 
LVL 24

Accepted Solution

by:
shivsa earned 125 total points
ID: 9829628
#!/usr/bin/perl

require Exporter;

my ( $line_no,
     $one_line,
     $true,
     );

$line_no = 0;
print "Verification Startup \n";
@aa = ();
@aa = `cat /tmp/historydmp.txt`;
while ($line_no < @aa ){
$one_line = @aa[$line_no];
$true = grep /Verification/, $one_line ;
if ( $true == 1) {
print "@aa[$line_no-1] \n";
     }
$line_no = $line_no + 1;
}
0
 

Author Comment

by:jporgy
ID: 10319901
shivsa's answer is okay.
0
 

Author Comment

by:jporgy
ID: 10319907
My html screen is screwed, can the moderator award the answer to shivsa'?
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now