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
245 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an explanation of a simple data model to help parse a JSON feed
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

832 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