date capturing regex and storing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting date capturing regex and storing
# 1  
Old 12-21-2009
date capturing regex and storing

Hi all

I need help on how to store two or more date formates captured using regex from an input sentence in PERL ? For example, I have an input sentence consisting of two dates such as :

The departure date is August 12, 2009 and arrival date is 20.08.2009.

Now, I want to capture the two dates and store them into two different variables as

var1 = August 12, 2009
var2 = 20.08.2009


How can I do this in perl?
Any help is appreciated
Thanks in advance.
# 2  
Old 12-21-2009
Quote:
Originally Posted by my_Perl
Hi all

I need help on how to store two or more date formates captured using regex from an input sentence in PERL ? For example, I have an input sentence consisting of two dates such as :

The departure date is August 12, 2009 and arrival date is 20.08.2009.

Now, I want to capture the two dates and store them into two different variables as

var1 = August 12, 2009
var2 = 20.08.2009


How can I do this in perl?
Any help is appreciated
Thanks in advance.
Code:
 perl -wln -e 'print $1," ",$2,"\n",$3 if /^.*(Jan|Feb|Mar|Apr)\s([0-9]{2},\s[0-9]{4}).*([0-9]{2}\.[0-9]{2}\.[0-9]{4})\.$/'

1.Replace Jan with January or whatever you month format is
2.Put all the months in the list . I havent put all of them e.g after April, May.....
3.Your question is too specific. Are all the lines that you want to process have the same format?
Regards.
# 3  
Old 12-21-2009
Code:
$inp="The departure date is August 14, 2009 and arrival date is 20.08.2009";


if ( $inp=~/.*\s(.* \d{2},\s\d{4}).*(\d{2}\.\d{2}\.\d{4})/ )
{
print $1 , "\n", $2 ;
}

# 4  
Old 12-21-2009
Code:
3.Your question is too specific. Are all the lines that you want to process have the same format?

It may not be in the same format. There may be only one date or no date in a sentence.It can be any of the common date format in use. Also, I am picking the sentences from a file using default $_ .Pl help me on how to handle this.
# 5  
Old 12-21-2009
Quote:
Originally Posted by my_Perl
Code:
3.Your question is too specific. Are all the lines that you want to process have the same format?

It may not be in the same format. There may be only one date or no date in a sentence.It can be any of the common date format in use. Also, I am picking the sentences from a file using default $_ .Pl help me on how to handle this.
Hello ,
my answer is wrt to $_ only the one by penchal bodhu is you can simply remove "$inp=~" and it would work.
In that case with varying data. you should give the varying inputs of which different solutions can be put up depending on the different patterns. Thats why I said that you gave only one set of i/p and designing a soln for it becomes too specific to work on a wide range of data or figures.
Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

2. Shell Programming and Scripting

Regex match date and seconds format

Hi $ awk '{print $1," ",$4}' access.log | sort | uniq -c| sort -nr | head -n20 62 192.168.10.6 How can get the result like 62, 192.168.10.6, 14:40 62, 192.168.10.32, 47:57 I tried modifying - $ awk '{print $1," ",$4}' access.log | sort | uniq -c| sort -nr | head -n20 | awk... (3 Replies)
Discussion started by: ashokvpp
3 Replies

3. Shell Programming and Scripting

Regex - Capturing groups

I am having trouble with regex capturing groups, For Ex : I am having a file with ABC CDLF SFSDFK PRIMARY INDEX(XYZ,DEF,GHI); XYZ FLJ SDFKLD; PRIMARY INDEX(ABC); BHI SDKFLFLSFD PRIMARY INDEX (QWE , RTY , LHJ); My output should be : ABC XYZ,DEF,GHI XYZ ABC BHI ... (10 Replies)
Discussion started by: ysvsr1
10 Replies

4. Shell Programming and Scripting

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'... (5 Replies)
Discussion started by: giridhar276
5 Replies

5. UNIX for Dummies Questions & Answers

regex date format help

I need to write a regular expression for todays date in format 12/11/11? any help will be greatful thanks. (3 Replies)
Discussion started by: drew211
3 Replies

6. Shell Programming and Scripting

Capturing regex of perl

Hi all I am struggling to find out the capturing regex of a date format such as 10/12/2009. Also I need help on how to assign the date(i.e, 10/12/2009 ) to a variable after the match is found using the capturing regex. Any help is appreciated. Thanks in advance. (5 Replies)
Discussion started by: my_Perl
5 Replies

7. UNIX for Dummies Questions & Answers

Retreiving and storing date...

First of all want to apologize for such a simple question. Very "new" to UNIX and have just taken a small intro class. I need to pull back YYYYMMDD and store it in a field to be used later. I figured out date "+%Y%m%d" returns the date in that format, just not sure how to store it. I am... (7 Replies)
Discussion started by: cards0622
7 Replies

8. 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

9. Programming

storing date into a file from a program

hi all: i want to store the current date in to a file from a program. every time i execute the prg the date should get appended into the file. help me plz (2 Replies)
Discussion started by: bankpro
2 Replies

10. Shell Programming and Scripting

Capturing previous date in UNIX

Hi All, Hope you can help me out here...This is what I'm doing: % date '+%y.%m.%d' 05.03.05 How can I tweak this so that it gives me the PREVIOUS date, more specifically: 05.03.04 Hope you all can help. Thanks!!! Vince (3 Replies)
Discussion started by: tomeldan
3 Replies
Login or Register to Ask a Question