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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adoquery sql  left join does not work 25 80
program a usb light to turn on when needed 6 49
Plain Text Editor for iPad 6 60
simplest php form 3 61
RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

920 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