Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
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?
Progress
Introduction to Processes

664 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