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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl : searching for month and storing the date and time in an array
# 1  
Old 09-03-2012
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' .

Code:
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
# 2  
Old 09-03-2012
Here is an example to try:
Code:
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

# 3  
Old 09-03-2012
thanks for replying..

but your code doesnt suit my requirement.In your code, you mentioned time as a variable.
Code:
$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.

Code:
### 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 ....
# 4  
Old 09-03-2012
Hi,

what about this..?

Code:
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 Smilie

Last edited by rangarasan; 09-03-2012 at 07:28 AM.. Reason: format
This User Gave Thanks to rangarasan For This Post:
# 5  
Old 09-03-2012
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....

Code:
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
# 6  
Old 09-03-2012
perl

Quote:
Originally Posted by giridhar276
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....

Code:
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
Code:
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 Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add decimal month to some month in sql, php, perl, bash, sh?

Hello, i`m looking for some way to add to some date an partial number of months, for example to 2015y 02m 27d + 2,54m i need to write this script in php or bash or sh or mysql or perl in normal time o unix time i`m asking or there are any simple way to add partial number of month to some... (14 Replies)
Discussion started by: bacarrdy
14 Replies

2. Shell Programming and Scripting

Storing the Linux command output to an array in perl script

Hi I am trying to store the output of a command into an array in perl script. I am able to store but the problem is i am unable to print the array line with one line space. i mean i inserted the \n in loop ...but not getting the result. I have written like this #!/usr/bin/perl @a =... (2 Replies)
Discussion started by: kumar85shiv
2 Replies

3. Shell Programming and Scripting

Help with getting last date of previous month and first date of previous 4th month from current date

I have requirment to get last date of previous month and the first date of previous 4th month: Example: Current date: 20130320 (yyyymmdd) Last date of previous month: 20130228 (yyyymmdd) First date of previous 4th month: 20121101 (yyyymmdd) In my shell --date, -d, -v switches are not... (3 Replies)
Discussion started by: machomaddy
3 Replies

4. Shell Programming and Scripting

Storing data in perl 2D array

Respected All, Kindly help me out. I have got file listings in a directory like this: -rw-r--r-- 1 root root 115149 2011-11-17 07:15 file1.stat.log -rw-r--r-- 1 root root 115149 2011-11-18 08:15 file2.stat.log -rw-r--r-- 1 root root 115149 2011-11-19 09:15 file3.stat.log -rw-r--r-- 1... (2 Replies)
Discussion started by: teknokid1
2 Replies

5. Shell Programming and Scripting

every time user input create array perl

Hi, How to create array every time user input and store user input and display all array print " Enter input " my @input = split(' ', $input) chmop($input = <STDIN>; foreach ($input) { @array= @input; } print @array"\n"; (1 Reply)
Discussion started by: guidely
1 Replies

6. UNIX for Dummies Questions & Answers

print previous month (current month minus 1) with Solaris date and ksh

Hi folks month=`date +%m`gives current month Howto print previous month (current month minus 1) with Solaris date and ksh (7 Replies)
Discussion started by: slashdotweenie
7 Replies

7. Shell Programming and Scripting

How to find the create time of a file if current date is in next month

Hi All, I want to find the time diffrence between currnt time and "abc.txt" file create time. I have solve that but if the abc.txt file created last month then is there any process to find the difftent? Exp: Create time of abc.txt is "Apr 14 06:48" and currect date is "May 17 23:47".... (1 Reply)
Discussion started by: priyankak
1 Replies

8. Shell Programming and Scripting

Searching array of arrays in perl

Suppose there are two arrays of arrays: @A = ( , , , ); @B = ( , , , , ); For each of $A, $A, $A..., I want to find the corresponding one in @B (match the letter, like $A eq $B), and print out both the second item, for example, $A and $B. How can I do this in perl? grep + map? Hope I... (1 Reply)
Discussion started by: zx1106
1 Replies

9. Shell Programming and Scripting

perl: storing regex in array variables trouble

hi this is an example of code: use strict; use warnings; open FILE, "/tmp/result_2"; my $regex="\\ Starting program ver. (.*)"; my $res="Program started, version <$1> - OK.\n"; while (<FILE>) { if ($_ =~ /($regex)/) { print "$res"; } } close FILE; This finds $regex and print... (3 Replies)
Discussion started by: xist
3 Replies

10. Shell Programming and Scripting

sed - searching for string and storing in variable

Hi I'm trying to find a way to search a text file for a specific string. I have a file which contains i.p. addresses and port numbers in the following format: 'ip="www.xxx.yyy.zzz"' 'port="xx""' I want to print only the parts between the double quotes for use in seperate variables,... (4 Replies)
Discussion started by: melias
4 Replies
Login or Register to Ask a Question