Hi
Currently I can grep a log file with the following command:
$results = `grep -A 2 '^$date.$time.*' $log`;
and the following arguments:
$date = 2007/04/25
$time = 16:07
Log example:
2007/04/25 16:07:12.145701
2007/05/25 14:07:12.145701
2007/05/25 17:07:12.145701
2007/06/25 16:07:12.145701
Therefore running the script ./script 2007/04/25 16:07
will return the 2nd line of the log - 2007/05/25 14:07:12.145701
But how do I do range of dates, by giving a 2 more arguments:
./script 2007/03/20 15:13 2007/08/19 14:31
So therefore returning:
2007/04/25 16:07:12.145701
2007/05/25 14:07:12.145701
2007/05/25 17:07:12.145701
I dont see how I can do that with grep as it is just pattern matching once, do I need a loop which can step through all the days and seconds in between the 2 dates or is there a kind of date range function?
Thanks
Rich
non trivial, grep will not cope
awk '$0>=from&&$0<=to' from="2007/03/20 15:13" to="2007/08/19 14:31" infile
Use nawk on Solaris.
im using, is this compatible:
SuSE Linux 9.3 (i586)
VERSION = 9.3
'$0>=from&&$0<=to' is ensuring that the $0 is in between the from and 2 dates specified.
How can I run this in perl?
Im using:
$results = `awk '$0>=from&&$0<=to' from="$date $time" to="$date2 $time2" $log `;
then "if (!$results) {" to find out if $results contains anything? but is not working
error is:
awk: ./s>=from&&./s<=to
awk: ^ syntax error
yes is working in the shell (command prompt) but not when running as part of perl script?.
How can I incorporate that into my perl script?
Write it in perl (not awk) ...
hmm yes......anyway
I dont know how, hence asking here.
I understand that
I don't know perl, so I'm not answerin' your question.
I'm just tryin' to say that if it's a perl script,
you should write it all in perl (as far as I know, it's quite feature-rich and powerful).
P.S. I shoud have noticed -> $results= ...
anbu23
June 29, 2007, 8:05am
10
perl -ne ' print if ( $_ >="2007/03/20 15:13" && $_ <= "2007/08/19 14:31" ) ' filename
OK, that also works from the command prompt.
But I am trying to incorporate it into my script so therefore:
I need to assign the results of the search to a variable.
Do I need to assign to an array or can I assign the search results to 1 variable
$results = `print if ( $_ >="2007/03/20 15:13" && $_ <= "2007/08/19 14:31" ) ` $log;
anbu23
June 29, 2007, 8:34am
12
epiphone:
OK, that also works from the command prompt.
But I am trying to incorporate it into my script so therefore:
I need to assign the results of the search to a variable.
Do I need to assign to an array or can I assign the search results to 1 variable
$results = `print if ( $_ >="2007/03/20 15:13" && $_ <= "2007/08/19 14:31" ) ` $log;
#!/usr/bin/perl
while( <> )
{
$results = $results . $_ if ( $_ >="2007/03/20 15:13" && $_ <= "2007/08/19 14:31" )
}
oh where has reading in the file gone?
In the above code, where I am specifying my log file to read from, in your previous codes "filename"?
anbu23
June 29, 2007, 8:58am
16
Script.pl
#!/usr/bin/perl
while( <> )
{
$results = $results . $_ if ( $_ >="2007/03/20 15:13" && $_ <= "2007/08/19 14:31" )
}
Run the script with your filename as
Script.pl your_log_filename
ahh ok I get it. Last question now hopefully.
I already have assigned a variable to the name of the log = $log
so instead of the while (<>) which is just reading from the argument, how can I change this for $log?
anbu23
June 30, 2007, 3:56am
18
epiphone:
ahh ok I get it. Last question now hopefully.
I already have assigned a variable to the name of the log = $log
so instead of the while (<>) which is just reading from the argument, how can I change this for $log?
#!/usr/bin/perl
$log="filename";
open FILE , $log;
while( <FILE> )
{
$results = $results . $_ if ( $_ >="2007/03/20 15:13" && $_ <= "2007/08/19 14:31" )
}
just realised this is not working at all, just gives me back every line and not filtering by the date and time etc
runing:
perl -ne ' print if ( $_ >="2007/05/20 15:13" && $_ <= "2007/08/19 14:31" ) ' filename
2007/05/25 15:51:36.957753
2007/05/25 15:51:36.958117
2007/05/25 15:56:06.670401
2007/05/25 15:56:06.670683
2007/05/25 16:07:12.145404
2007/05/25 16:07:12.145701
2007/04/14 16:07:12.145701
u can see that im getting the last entry too - 2007/04/14 16:07:12.145701
the log file actually contains more than just the date and time of course
2007/05/25 15:51:36.957753 dsfsdf dsfdfdf-sdfdfdf Trace name: sfsdfdsf
So shall i just add .* after the time like with regex?