perl : searching for month and storing the date and time in an array

I am writing the code in perl.

I have an array in perl and each variable in the array contains the data in the below format
Now I need to check the below variable w.r.t system month I need to store the date and time(Tue Aug 7 03:54:12 2012) from the below data into file if contains only 'Aug' .

Created By = sripathi
Server Name = dysp001
time = Tue Aug  7 03:54:12 2012
Node = mumgwmpls2335.net.hca.com

Could anyone please let me know how to store the time(Tue Aug 7 03:54:12 2012).

Thanks in advance....

Regards,
GS

---------- Post updated 09-03-12 at 12:00 AM ---------- Previous update was 09-02-12 at 11:51 PM ----------

Actually the problem is that if the above data is a text file then we can do it easily but I have the data stored in a variable and there lies the problem.

Could anyone please let me know how to solve this.

Thanks in adavnce...

Regards,
GS

Here is an example to try:

if ($time ~= m/aug/) {
....do what you want
}
$time = "Tue Aug  7 03:54:12 2012"; if ($time =~ m/aug/i) {print "$time\n;"}
Tue Aug  7 03:54:12 2012

thanks for replying..

but your code doesnt suit my requirement.In your code, you mentioned time as a variable.

$time = "Tue Aug  7 03:54:12 2012"; if ($time =~ m/aug/i) {print "$time\n;"} Tue Aug  7 03:54:12 2012

But if you look into my below data...time is not a variable. It is just a text inside the data.

### below mentioned data is not in a text file... It is stored in a array variable
Created By = sripathi 
Server Name = dysp001 
time = Tue Aug  7 03:54:12 2012 
Node = mumgwmpls2335.net.hca.com

for better understanding ...
the above mentioned data is stored in a variable say $data.
Now I need to search for the aug month. If the data contains aug then I need to store ONLY the "Tue Aug 7 03:54:12 2012" into a separate variable.

Could you please help me ....

Hi,

what about this..?

my $data="Created By = sripathi Server Name = dysp001 time = Tue Aug 7 03:54:12 2012 Node = mumgwmpls2335.net.hca.com";
if($data=~/aug/i){
    $res=$data;
    $res=~s/.*time =(.*)Node =.*/$1/;
    print "Original:$data\nResult:$res\n";
}

Cheers,
Ranga :slight_smile:

1 Like

Thanks alot Ranga..

Now I am able to get the required data.

Could you please format the query for the below data.
The output should be same..
I need to copy the data and time(Tue Aug 7 03:54:12 2012) to another variable....

Created By = sumitp
ServerName = dysp001
FirstOccurrence = Tue Aug  7 03:54:12 2012
ServerSerial = 145924222
Summary = DEVICE HAS STOPPED RESPONDING
Customer = TELECOM
Node = mumabcsbp5034.net.adp.com
Severity = Critical

Thanks in advance...

Regards,
GS

open(FILE,"<filename");

while(<FILE>) {
  my $data=$_;
  chomp($data);
  if($data=~/ aug /i){
      $res=$data;
      $res=~s/.* = (.*)/$1/;
      print "Original:$data\nResult:$res\n";
  }
  # Do your other stuff here with the date and time.
}
close(FILE);

Here I am assuming that the data is in a file called 'filename' and read that file and do other stiff with that date.

Cheers,
Ranga :slight_smile: