The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
combine two perl lines into a single perl command jimmy_y Shell Programming and Scripting 4 06-15-2009 12:56 AM
[Perl] Insert lines before lines. ejdv Shell Programming and Scripting 7 06-12-2009 04:12 AM
Search for different lines in 2 files khestoi UNIX for Dummies Questions & Answers 5 12-09-2008 01:40 PM
search for lines in a file shalua Shell Programming and Scripting 9 04-23-2008 12:28 AM
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 10:24 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 3 Weeks Ago
bataf bataf is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 6
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.
Bataf


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

Code:
 
while( $line = <FILE>){
 chomp ($line);
  my ($fdate,undef)= split(" ",$line);
 #print "$fdate $cdate\n";
 if ($fdate =~ /$curr_date/  && $line =~ m/TV)  {
  $tv++;
  #$nextline = <FILE>;
  my $next =<FILE>;
  
  if ($next =~ m/W_NO/){
   print "$next\n";
   $line = $next
   
   }
  }
  #2 (permalink)  
Old 3 Weeks Ago
daptal daptal is online now
Registered User
  
 

Join Date: Mar 2009
Posts: 61
Code:

use Date::Calc qw/Delta_YMDHMS/;

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

my ($start_date,$start_time,$end_date,$end_time);
while(<$fh>) {
        chomp;
        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

HTH,
PL
  #3 (permalink)  
Old 3 Weeks Ago
TonyLawrence TonyLawrence is offline
Registered User
  
 

Join Date: Sep 2007
Location: SE Mass
Posts: 146
I'm not sure what you want. Is yjhos close?

Code:
#!/usr/bin/perl
$tv=0;
$wo=0;
@start=();
@stop=();
while (<>) {
  chomp;
  @stuff=split /\s/;
  if (/TV/ ) {
    $start[$tv++]="$stuff[0] $stuff[1]";
  }
  if (/W_NO/) {
    $stop[$wo++]="$stuff[0] $stuff[1]";
  }
}
$x=0;
foreach (@start) {
  print "$_ $stop[$x++]\n";
}
  #4 (permalink)  
Old 3 Weeks Ago
rdcwayx rdcwayx is online now
Registered User
  
 

Join Date: Jun 2006
Posts: 256
Code:
$ awk '/TV/,/W_NO/' urfile |awk -F[\"] '/value/ {print $2}'
A00005FF.rf
A00008FF.rf
A070058FF.rf
A077758FF.rf
  #5 (permalink)  
Old 3 Weeks Ago
bataf bataf is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 6
Thank you all for the reply...
Daptal your program does exatctly what I want..exept i get error Date::Calc:elta_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,,,
bataf

Last edited by bataf; 3 Weeks Ago at 02:35 AM..
  #6 (permalink)  
Old 3 Weeks Ago
daptal daptal is online now
Registered User
  
 

Join Date: Mar 2009
Posts: 61
Quote:
Originally Posted by bataf View Post
Thank you all for the reply...
Daptal your program does exatctly what I want..exept i get error Date::Calc:elta_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"'

bataf

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

HTH,
PL
  #7 (permalink)  
Old 3 Weeks Ago
bataf bataf is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 6
Thanks daptal, I got it and you helped me alot...
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:38 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0