perl regular expression format date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl regular expression format date
# 1  
Old 01-25-2010
perl regular expression format date

Hi Everyone,
Code:
Mon 18 Jan 2010 09:52:10 AM MYT

the output is
Code:
20100118 09:52:10

how to do it in perl regular expression =~

Thanks
# 2  
Old 01-25-2010
Quote:
Originally Posted by jimmy_y
Hi Everyone,
Code:
Mon 18 Jan 2010 09:52:10 AM MYT

the output is
Code:
20100118 09:52:10

how to do it in perl regular expression =~

Thanks
Hello try this,

Code:
gaurav@localhost:~$ echo 'Mon 18 Jan 2010 09:52:10 AM MYT' | perl -wln -e 'my @arr=split(/\s/,$_);my %year=("jan"=>"01","feb"=>"02","mar"=>"03");print $arr[3],$year{lc $arr[2]},$arr[1]," ",$arr[4];'
20100118 09:52:10
gaurav@localhost:~$

Also add to the hash list like "apr" => "04" ,"may"=>"05" etc . I have left it for you to complete the list for a wider result.

Regards,
Gaurav.

Last edited by gaurav1086; 01-25-2010 at 06:48 AM..
# 3  
Old 01-25-2010
You can't do that with just an Regex, because of the required transformation of a three-letter month to a number. However, with an suitable lookup hash, it becomes almost trivial:
Code:
my %mon = ( 'Jan' => 1, 'Feb' => 2, 'Mar' => 3, 'Apr' => 4, 'May' => 5, 'Jun' => 6, 'Jul' => 7,
    'Aug' => 8, 'Sep' => 9, 'Oct' => 10, 'Nov' => 11, 'Dec' => 12, );
my $str = 'Mon 18 Jan 2010 09:52:10 AM MYT';
my @dt = ( $str =~ /^.*? (\d+) (\w+) (\d{4}) (\d{2}:\d{2}:\d{2})/ );
my $date = sprintf '%4d%02d%02d %s', $dt[2], $mon{$dt[1]}, $dt[0], $dt[4];

Or, you can use a module like Date::Parse for it.
# 4  
Old 01-26-2010
Quote:
Originally Posted by pludi
You can't do that with just an Regex, because of the required transformation of a three-letter month to a number. However, with an suitable lookup hash, it becomes almost trivial:
Code:
my %mon = ( 'Jan' => 1, 'Feb' => 2, 'Mar' => 3, 'Apr' => 4, 'May' => 5, 'Jun' => 6, 'Jul' => 7,
    'Aug' => 8, 'Sep' => 9, 'Oct' => 10, 'Nov' => 11, 'Dec' => 12, );
my $str = 'Mon 18 Jan 2010 09:52:10 AM MYT';
my @dt = ( $str =~ /^.*? (\d+) (\w+) (\d{4}) (\d{2}:\d{2}:\d{2})/ );
my $date = sprintf '%4d%02d%02d %s', $dt[2], $mon{$dt[1]}, $dt[0], $dt[4];

Or, you can use a module like Date::Parse for it.

Oh, thanks Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Regular Expression in Date ls command

Hi, I have got a problem in a regular expression with a file name containing date. I am using a regular to display the file in ls, but by using the same it gives me file name does not exist. --## File exist when I do ls. ls amey_in20131018.csv --## File name not showing when I use... (5 Replies)
Discussion started by: ameyrk
5 Replies

2. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

3. Shell Programming and Scripting

Hidden Characters in Regular Expression Matching Perl - Perl Newbie

I am completely new to perl programming. My father is helping me learn said programming language. However, I am stuck on one of the assignments he has given me, and I can't find very much help with it via google, either because I have a tiny attention span, or because I can be very very dense. ... (4 Replies)
Discussion started by: kittyluva2
4 Replies

4. Shell Programming and Scripting

Required help in perl regular expression substitution for this date format

Hi, I have written a small perl script to handle particular date format using perl, but it is not substituting the whole string. Can some one please check on what is the issue with the code. $_ = "Date: November 25, 2010 09:02:01 PM";... (1 Reply)
Discussion started by: sarbjit
1 Replies

5. Shell Programming and Scripting

Perl Regular Expression

Hello, I am trying to use perl LWP module to read and get a specfic URL page. The issue is that the URL ends with the data and time and time is not consistent it changes all the time. if anyone could help me how to write a regular expressin that would work in the LWP::UserAgent get function to... (0 Replies)
Discussion started by: bataf
0 Replies

6. Shell Programming and Scripting

Need perl regular expression

Hi, I am looking for a Perl regular expression to match the below pattern of a java script file. var so = object.device.load('camera','value'); I want to grep out such lines present in the *.js files. The conditions are: a) the line may start with blank space(s) b) always the... (3 Replies)
Discussion started by: royalibrahim
3 Replies

7. Shell Programming and Scripting

validate date pattern using Regular Expression

Hi, i am java guy and new to unix. I want to validate date pattern using Regex expression here is the sample program i have written. #!/bin/sh checkDate="2010-04-09" regex="\\d{4}-\\d{2}-\\d{2}\$" echo $regex if ] then echo "OK" else echo "not OK" fi But the ouput is... (2 Replies)
Discussion started by: vvenu88
2 Replies

8. Shell Programming and Scripting

regular expression format string in one line.

Hi All, @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); $day=091023; $day_combine = $day; $day_combine =~ s/({2})({2})({2})/20$1-$months-$3/; Instead of three lines, is possible to combine the last two lines into a single line? means no need assign $day to $day_combine... (2 Replies)
Discussion started by: jimmy_y
2 Replies

9. Shell Programming and Scripting

PERL regular expression

Hello all, I need to match the red expressions in the following lines : MACRO_P+P-_scrambledServices_REM_PRC30.xml MACRO_P+P-_scrambledServices_REM_RS636.xml MACRO_P+P-_scrambledServices_REM_RS535.xml and so on... Can anyone give me a PERL regular expression to match those characters ? ... (5 Replies)
Discussion started by: lsaas
5 Replies

10. Shell Programming and Scripting

regular expression format

. . . AA = 0.000000, 0.000000, 0.006000, 0.006000, 1, 1.000000, 0.000000 ;item 1 #line 1 BB = -0.002990, -0.002990, 0.002990, 0.002990, 0, 1.000000, 0.000000 ;List 1 #line 2 CC = 0.023620, 0.023620, 0.035430, 0.035430, 1, 1.000000, 0.000000 ;Counter strike ... (1 Reply)
Discussion started by: trynew
1 Replies
Login or Register to Ask a Question