PERL String to Date (Custom format yyyymmdd to dd-mon-yyyy)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL String to Date (Custom format yyyymmdd to dd-mon-yyyy)
# 1  
Old 03-05-2010
Question PERL String to Date (Custom format yyyymmdd to dd-mon-yyyy)

Hi All,

I am learning PERL for one of the projects, and in one of these scripts, I read a flat text file and print in the terminal.

The problem is, the text file has a date field. The format is yyyymmdd. I need to display this as dd-mon-yyyy.

Any ideas to do this? Thanks a lot for the help.

Regards
Guru
--------------------------------------------------------------------------
Note: I wrongly tagged this as PERL DATE::MANIP. Looks like I need to download and use this. Is it not included in standard distribution. If not, I can't use it to. Smilie

Last edited by guruparan18; 03-05-2010 at 06:16 AM.. Reason: Added note
# 2  
Old 03-05-2010
We can easily do that by using regular expression in Perl.

Code:
$line="20091029 Text File";
$line=~/([0-9]{4})([0-9]{2})([0-9]{2}).*/;
print "$3-$2-$1";

# 3  
Old 03-05-2010
Code:
open FH,"<date_file";
my $full_date = <FH>;
chomp($full_date);
if ( $full_date =~ /^([0-9]{4})([0-9]{2})([0-9]{2})/)
{
        print "$3-$2-$1\n";
}

consider that the date_file is having the data as
20100305 which is in yyyymmdd format.

Now the above script's print statement will print the output as
05-03-2010 which is in the dd-mm-yyyy format.

Last edited by thillai_selvan; 03-05-2010 at 06:24 AM.. Reason: closed open tag
# 4  
Old 03-05-2010
MySQL

You can do this simply by the substitution.

Code:
use strict;
use warnings;
open FH , "<test"; # test is the file 
while(<FH>)
{
    $_=~s/([0-9]{4})([0-9]{2})([0-9]{2})/$3-$2-$1/g;
    print $_;
}

# 5  
Old 03-05-2010
If the regex

Quote:
([0-9]{4})([0-9]{2})([0-9]{2})/)
means 20100205 to 05-02-2010, then it is wrong. I am looking for 05-FEB-2010.
# 6  
Old 03-05-2010
For this,you can use the hash in your perl program.

Code:
%hash=("01"=>"Jan","02"=>"Feb","03"=>"Mar","04"=>"Apr","05"=>"May","06"=>"Jun","07"=>"Jul","08"=>"Aug","09"=>"Sep","10"=>"Oct","11"=>"Nov","12"=>"Dec");
$line="20090329 Text File";
$line=~/([0-9]{4})([0-9]{2})([0-9]{2}).*/;
print "$3-$hash{$2}-$1";

# 7  
Old 03-05-2010
Code:
use strict;

open FH , "<test";

my %hash=("01"=>"JAN","02"=>"FEB","03"=>"MAR","04"=>"APR","05"=>"MAY","06"=>"JUN","07"=>"JUL","08"=>"AUG","09"=>"SEP","10"=>"OCT","11"=>"NOV","12"=>"DEC");
while(<FH>)
{

     if(/([0-9]{4})([0-9]{2})([0-9]{2})/)
    {
    print $3."-".$hash{$2}."-".$1 ."\n";
    }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Convert string (YYYYMMDD) format to date in Sun OS

Hi All I need help in converting a string of YYYYMMDD format to date in Sun OS and then find out if the day is a Wednesday or not. The "date -d" option is not working and your help is much appreciated. The date command usage from the operating system we use here is as follows: Thanks, SK (11 Replies)
Discussion started by: SK123
11 Replies

2. Shell Programming and Scripting

Convert string (YYYYMMDD) format to date in Sun OS

Hi All I need help in converting a string of YYYYMMDD format to date in Sun OS and then find out if the day is a Wednesday or not. The "date -d" option is not working and your help is much appreciated. The date command usage from the operating system we use here is as follows: usage: ... (1 Reply)
Discussion started by: SK123
1 Replies

3. Programming

Date format change from mm/dd/yyyy to yyyymmdd in comma seperate line in perl

Hi All, I have line ,A,FDRM0002,12/21/2017,,0.961751583,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, it contains date in mm/dd/yyyy format i want to change this to yyyymmdd format using perl. Use code tags, thanks. (8 Replies)
Discussion started by: vishal0746
8 Replies

4. Shell Programming and Scripting

Date conversion help from dd/mm/yyyy to dd/Mon/yyyy i.e. 28/10/2012 to 28/Oct/2012

Hi I have a problem with Date format in my code. 1st I am trying to convert today's date to yesterday's using YESTERDAY3=`perl -e '@y=localtime(time()-86400); printf "%04d/%02d/%02d",$y+1900,$y+1,$y;$y;'` And once it is done I am trying to using the yesterday date in a grep command to... (3 Replies)
Discussion started by: nithinankam
3 Replies

5. Shell Programming and Scripting

Validating date in yyyymmdd format using PERL

Hi all, i had a code where in user will enter a date in yyyymmdd format.. i didnt use any validation for the date and now the problem is if a user enters date instead of month after year it is proceeding with the code.. like if the date is 20120426 and if the user enters 20122604 it... (4 Replies)
Discussion started by: smarty86
4 Replies

6. Shell Programming and Scripting

Converting Date from YYYYMMDD to DD-MON-YYYY

Hi , I need to convert date from YYYYMMDD to DD-MON-YYYY e.g 20111214 to 14-Dec-2011 Please help. (17 Replies)
Discussion started by: ady_koolz
17 Replies

7. Shell Programming and Scripting

Converting date DD MM YYYY to DD MON YYYY

Hello, I am writing a script that parses different logs and produces one. In the source files, the date is in DD MM YYYY HH24:MI:SS format. In the output, it should be in DD MON YYY HH24:MI:SS (ie 25 Jan 2010 16:10:10) To extract the dates, I am using shell substrings, i.e.: read line ... (4 Replies)
Discussion started by: Adamm
4 Replies

8. Shell Programming and Scripting

convert date format YYYYMMDD to MM/DD/YYYY

In my shell script i have a variable which stores date in the format of YYYYMMDD. Is there any way to format this value to MM/DD/YYYY. Thanks. (8 Replies)
Discussion started by: nasirgondal
8 Replies

9. UNIX for Dummies Questions & Answers

Format date from MM/DD/YYYY to YYYYMMDD

I have a file with some date columns in MM/DD/YYYY format: SMPBR|DUP-DO NOT USE|NEW YORK||16105|BA5270715|6/6/2007 |MWERNER|109||||JOHN||SMITH|MD|72211118||||||74559|21 WILMINGTON RD||D|11/6/2003|SL# MD CONTACT-LIZ RICHARDS|||0|Y|N||1411458| And I want to convert the date format to: ... (5 Replies)
Discussion started by: ChicagoBlues
5 Replies

10. UNIX for Dummies Questions & Answers

how to convert the string YYYYMMDD into YYYY.MM.DD

how to convert the string YYYYMMDD into YYYY.MM.DD Please advice (1 Reply)
Discussion started by: spatra
1 Replies
Login or Register to Ask a Question