Visit Our UNIX and Linux User Community

Perl to Search through next lines

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl to Search through next lines
# 1  
Old 11-03-2009
Perl to Search through next lines

I have log file that I need to extract time difference occurance when two events happend, between first occurance of TV and when W_NO happend. also read the value=, below example...I can only read the next line but not able to seach all the next lines untill i see W_NO..

Thanks for your help.

11/03/2009 07:28:27 TV
11/03/2009 07:28:35 TV
11/03/2009 07:28:37
11/03/2009 07:38:30
11/03/2009 07:39:00 W_NO value="A00005FF.rf"
11/03/2009 07:40:40 TV
11/03/2009 07:40:40
11/03/2009 07:40:42
11/03/2009 07:40:44 W_NO value="A00008FF.rf"
11/03/2009 07:42:57
11/03/2009 07:45:08
11/03/2009 07:45:08 W_NO value="A0058FF.rf"
11/03/2009 07:45:10
11/03/2009 07:45:12
11/03/2009 07:51:47 TV
11/03/2009 07:53:37
11/03/2009 07:53:38
11/03/2009 07:53:38 W_NO value="A070058FF.rf"
11/03/2009 07:55:49
11/03/2009 07:55:49
11/03/2009 07:57:59 W_NO value="A00008FF.rf"
11/03/2009 07:57:59
11/03/2009 08:00:10 TV
11/03/2009 08:00:10 W_NO value="A077758FF.rf"
11/03/2009 08:02:20

while( $line = <FILE>){
 chomp ($line);
  my ($fdate,undef)= split(" ",$line);
 #print "$fdate $cdate\n";
 if ($fdate =~ /$curr_date/  && $line =~ m/TV)  {
  #$nextline = <FILE>;
  my $next =<FILE>;
  if ($next =~ m/W_NO/){
   print "$next\n";
   $line = $next

# 2  
Old 11-03-2009
use Date::Calc qw/Delta_YMDHMS/;

open my $fh , '<' ,"abc.txt" || die "$!";

my ($start_date,$start_time,$end_date,$end_time);
while(<$fh>) {
        if ($start_date){
                if (m/(\d{2}\/\d{2}\/\d{4})\s+(\d{2}\:\d{2}\:\d{2})\s+W_NO\s+value=(.*)$/){
                        $end_date = $1;
                        $end_time = $2;
                        my $value = $3;
                        print "$start_date\t$start_time \n$end_date\t$end_time \n";
                        my ($sdate,$smon,$syear) = split("\/",$start_date);
                        my ($edate,$emon,$eyear) = split("\/",$end_date);

                        my ($diff_year,$diff_mon,$diff_days, $diff_hours,$diff_min,$diff_sec) = Delta_YMDHMS($syear,$smon,$sdate,split(":",$start_time),$eyear,$emon,$edate,split(":",$end_time));
                        print "$value \t $diff_year,$diff_mon,$diff_days, $diff_hours,$diff_min,$diff_sec\n";
                        $start_date = '';
        else  {
                if (m/^(\d{2}\/\d{2}\/\d{4})\s+(\d{2}\:\d{2}\:\d{2})\s+TV$/){
                        $start_date = $1;
                        $start_time = $2;

I am not sure what you have tried. When in cases where in you donot know how to proceed , it is better to write down the logic in simple steps (ie write the algorithm) and implement it. That will most of the times simplify the whole problem

# 3  
Old 11-04-2009
I'm not sure what you want. Is yjhos close?

while (<>) {
  @stuff=split /\s/;
  if (/TV/ ) {
    $start[$tv++]="$stuff[0] $stuff[1]";
  if (/W_NO/) {
    $stop[$wo++]="$stuff[0] $stuff[1]";
foreach (@start) {
  print "$_ $stop[$x++]\n";

# 4  
Old 11-04-2009
$ awk '/TV/,/W_NO/' urfile |awk -F[\"] '/value/ {print $2}'

# 5  
Old 11-04-2009
Thank you all for the reply...
Daptal your program does exatctly what I want..exept i get error Date::Calc:Smilieelta_YMDHMS(): not a valid date when I use a day gerater then 11. for example 11/15/2009 07:53:38 will not calculate the difference...

Below the actual log file data, i am still trying to modify the reg expression to capture the phrasees within a long lines of various characters and numbers.

The algorithm for the script for log file from a machine:
1. Find when the first time TV error occures end of the line with unique phrase ="MMeas"'
2. The TV error will stop the machine and multiple error might occure afterwards, the script should skip these errors.
3. Find when the machine start back up (W_NO) and within the long line there is this unique phrase name="WWWW_NO" and the value="A333BBBB.rf"
4. the output of the script should show (calculate) when the first TV error happens and when the machine start back up. These events could happen through out the day.
5. Calculate the total time differences of all the events (of waiting between the first error and next machine start).

Best Regards,,,

Last edited by bataf; 11-05-2009 at 02:35 AM..
# 6  
Old 11-05-2009
Originally Posted by bataf
Thank you all for the reply...
Daptal your program does exatctly what I want..exept i get error Date::Calc:Smilieelta_YMDHMS(): not a valid date when I use a day gerater then 11. for example 11/15/2009 07:53:38 will not calculate the difference...
(example error happend, machine stoped) each time stamp is one line...
11/04/2009 00:04:01 Sent on '4CAN08_vf_out': 'COMMAND_ID="ALARM_REPORT" MACHINE_ID="FXAN08_vf" MSG_TYPE=E ERROR_CODE=0 ERROR_TEXT="" ALARM_ID=1000 ALARM_STATE=133 ALARM_TEXT="Main seq/TV alignment/Meas"'


Find out if the log file has the date format in dd/mm/yyyy or mm/dd/yyyy , depending on that you have to change the parameters you send to Date::Calc:: Delta_YMDHMS.

My script goes with the assumption that its in the format dd/mm/yy

# 7  
Old 11-05-2009
Thanks daptal, I got it and you helped me alot...

Previous Thread | Next Thread
Test Your Knowledge in Computers #83
Difficulty: Easy
BSD, Linux, Mac OS X, and other Unix-like operating systems did not inherit aspects of the original Unix design.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search pattern on logfile and search for day/dates and skip duplicate lines if any

Hi, I've written a script to search for an Oracle ORA- error on a log file, print that line and the .trc file associated with it as well as the dateline of when I assumed the error occured. In most it is the first dateline previous to the error. Unfortunately, this is not a fool proof script.... (2 Replies)
Discussion started by: newbie_01
2 Replies

2. Shell Programming and Scripting

Perl - start search by using search button or by pressing the enter key

#Build label and text box $main->Label( -text => "Input string below:" )->pack(); $main->Entry( -textvariable => \$text456 )->pack(); $main->Button( -text => "Search", -command => sub { errchk ($text456) ... (4 Replies)
Discussion started by: popeye
4 Replies

3. Shell Programming and Scripting

Perl - use search keywords from array and search a file and print 3rd field when matched

Hi , I have been trying to write a perl script to do this job. But i am not able to achieve the desired result. Below is my code. my $current_value=12345; my @users=("bob","ben","tom","harry"); open DBLIST,"<","/var/tmp/DBinfo"; my @input = <DBLIST>; foreach (@users) { my... (11 Replies)
Discussion started by: chidori
11 Replies

4. Shell Programming and Scripting

Search and swap multiple lines in file using Perl

Hi all, I have a vcd file with a bunch of lines containing an array, like this $var wire 1 b a $end $var wire 1 c a $end $var wire 1 d a $end $var wire 1 e a $end $var wire 1 f b $end $var wire 1 g b $end $var wire 1 h b $end $var wire 1 i b $end I want it like this: $var wire 1 e a... (12 Replies)
Discussion started by: veerabahu
12 Replies

5. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

6. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

7. Shell Programming and Scripting

Perl XML, find matching condition and grep lines and put the lines somewhere else

Hi, my xml files looks something like this <Instance Name="New York"> <Description></Description> <Instance Name="A"> <Description></Description> <PropertyValue Key="false" Name="Building A" /> </Instance> <Instance Name="B"> ... (4 Replies)
Discussion started by: tententen
4 Replies

8. Shell Programming and Scripting

Search in a set of lines using perl

Hi Experts, i am beginner in perl and need your help to find a solution.. I have a block of multiple lines like below say module 1 to module 100 ***** MAKING > module1 ************** kvmfkvmmfdv svksmnvlksmfvks dcsdvcs sddvcsv ssvsdvdf error: abcdefghi wrw wvsv dsvds sdvsd error:... (5 Replies)
Discussion started by: ganga.dharan
5 Replies

9. Shell Programming and Scripting

combine two perl lines into a single perl command

Hi Everyone, i have a string 00:44:40 so: $tmp=~ s/://gi; $tmp=~s/({2})({2})({2})/$1*3600+$2*60+$3/e; the output is 2680. Any way to combine this two lines into a single line? Thanks (4 Replies)
Discussion started by: jimmy_y
4 Replies

10. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

Featured Tech Videos