Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
275 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
5 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 500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…

824 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