![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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
}
}
|
|
||||
|
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;
}
}
}
HTH, PL |
|
||||
|
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";
}
|
|
||||
|
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.. |
|
||||
|
Quote:
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|