arsidh
May 22, 2008, 10:27am
1
Hi All,
I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl
FROM_DATE="06/05/2008"
TO_DATE="07/05/2008"
"perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename"
filename has following data :-
06/05/2008-07:59
06/05/2008-07:59
06/05/2008-07:59
06/05/2008-07:59
06/05/2008-07:59
07/05/2008-07:59
07/05/2008-07:59
07/05/2008-07:59
07/05/2008-07:59
07/05/2008-07:59:server5:DISK
08/05/2008-07:59:server1:DISK
08/05/2008-07:59:server2:DISK
08/05/2008-07:59:server3:DISK
08/05/2008-07:59:server4:DISK
08/05/2008-07:59:server5:DISK
if we hard code the values of FROM_DATE and TO_DATE it is working.I feel what to know why it is not working When i passing it as variable.
it will be great if some one telle me How to pass the variable to perl properly.
i am new to perl world.
Thanks
Arsidh
replace $FROM_DATE with $ENV{'FROM_DATE'}
replace $TO_DATE with $ENV{'TO_DATE'}
arsidh
May 23, 2008, 12:55am
3
Hi Yogesh,
Thanks for your valuable input, and Thanks for reply on it.
I have included the sugeestion , you told me, But When i run it , i am not getting any data, which i want, but the comment from the file.
Here is the Modified Code :
#!/bin/bash
FROM_DATE="04/05/2008"
TO_DATE="06/05/2008"
perl -ne 'print if ( $_ >= $ENV{'FROM_DATE'} && $_ <= $ENV{'TO_DATE'} )' monitor.audit
exit
The output is only the commented line
#Memory Usage
#Check the Availability
#Disk Monitor
#Load Monitor
#Memory Usage
#Check the Availability
The file monitor.audit data
04/05/2008-12:16:or:453
04/05/2008-12:16::77
04/05/2008-12:16::138
#Load Monitor
04/05/2008-12:16:ora:0.39
04/05/2008-12:16::9.01
04/05/2008-12:16::0.39
#Memory Usage
04/05/2008-12:16:oraprd01:13
04/05/2008-12:16::18
04/05/2008-12:16::18
#Check the Availability
04/05/2008-12:32:ora:453
04/05/2008-12:32::77
04/05/2008-12:32::138
#Load Monitor
Can you please , tell what i need to do
Thanks
Arsidh
Do this:
export FROM_DATE="04/05/2008"
export TO_DATE="06/05/2008"
Hi rikxik/Yogesh,
Cool Man , it is working with "export" cmd.
Thanks guys, you guys rocks.
Thanks a ton.
Arsidh
arsidh
June 2, 2008, 10:59am
6
Hi Guys,
Now have some other problem with Perl script.
The solution which is given is working perfectly , if the dates are with in same month.If i give dates like this
export FROM_DATE=04/05/2008
export TO_DATE=02/06/2008
perl -ne 'print if ( $_ >= "'$FROM_DATE'" && $_ <= "'$TO_DATE'" )' monitor.audit >$FINAL_DATA
Then nothing is comming in the log file.
monitor.audit
06/05/2008-10:24: orasdfprd01:DATABASE:gecolfdsafprd :1
06/05/2008-10:24: cisdsfsdmarsp010:DATABASE:mafdsrp :1
06/05/2008-10:24: sdfsdacismarsfp011:DATABASE:dxfdslp :1
06/05/2008-10:24: alpfdscisfdfspdb053:DATABASE:mbdsfsop :1
12/05/2008-10:24: cisfsdfmasdfdsfrsp020:APPLCATION :1
12/05/2008-10:24: cisdsfdsfmarsp021:APPLCATION :1
12/05/2008-10:24: alpcsdfdsfispapp054:DISCOVER :0
12/05/2008-10:24: alpcsdfdsispapp055:DISCOVER :1
12/05/2008-10:39: orapsdafdsafrd01:DISK : 456
12/05/2008-10:39: cismdsafsdaarsp010:DISK : 77
12/05/2008-10:39: cisfsdafdsamarsp011:DISK : 138
20/05/2008-10:39: cismsdafsdaarsp020:DISK: 28
20/05/2008-10:39: cismfsdfsdarsp021:DISK: 28
20/05/2008-10:39: alpcsdfdsispapp054:DISK: 190
20/05/2008-10:39: alpcifasfsdaspapp055:DISK: 190
20/05/2008-10:39: alpcfsdafsdaispdb053:DISK: 309
02/06/2008-10:39: orasdfdsprd01:LOAD: 1.05
02/06/2008-10:39: cisdsfdsmarsp010:LOAD:2.86
02/06/2008-10:39: cissdafdsmarsp011:LOAD:0.43
02/06/2008-10:39: cismasadfsdrsp020:LOAD:0.12
02/06/2008-10:39: cismarsdfdssp021:LOAD:0.08
02/06/2008-10:39: alpcisdsfdsapapp054:LOAD:0.44
================================
Please can any one suggest on this .
era
June 3, 2008, 1:13am
7
You should be aware, though, that Perl's <= and >= operators simply do numeric comparison. In other words, your script will not work without modification e.g. across a month boundary. The solution is to parse the date strings, and use the resulting raw numbers for comparison.
Oops, you noticed already. Anyway, Google for date parsing in Perl. Also perhaps read the time and date handling threads in the FAQs for these forums.
Thanks for the reply era. Sure i will the time and date handling threads. It is good to hear from you.
-Arsidh
#!/usr/bin/ksh
export FROM_DATE=04/05/2008
export TO_DATE=02/06/2008
export F=$(echo $FROM_DATE|awk -F"/" '{print $3$2$1}')
export T=$(echo $TO_DATE|awk -F"/" '{print $3$2$1}')
perl -ne '$l=$_;($d,$m,$y)=(split(/\//, (split(/-/, $l))[0] ))[0..2]; $dt="$y$m$d";
print $l if ($dt >= "'$F'" && $dt <= "'$T'" )' monitor.audit
HTH
era
June 4, 2008, 3:05am
10
You might as well do that too in Perl.
perl -ne '$l=$_;($d,$m,$y)=(split("/", (split(/-/, $l))[0] ))[0..2]; $dt="$y$m$d";
$f = join ("",(split ("/", $ENV{"FROM_DATE"}))[2,1,0]);
$t = join ("", (split ("/", $ENV{"TO_DATE"}))[2,1,0]);
print $l if ($dt >= $f && $dt <= $t )' monitor.audit
If the dates are mm/dd/yyyy ones, you can replace [2,1,0] with [2,0,1] (in both places, of course).
Even better, use yyyymmdd format for FROM_DATE and TO_DATE to begin with.
arsidh
June 4, 2008, 12:25pm
11
Thanks a ton for great help.You make my day.
arsidh