Converting dates to iso format


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Converting dates to iso format
# 1  
Old 10-17-2013
Lightbulb Converting dates to iso format

Hi ,

i am trying to read a tsv file record by record and change the date column with iso date format. it have different dates with format like mm/dd/yyyy HH:MM:SS EST ,i am trying serarch for the date format lke this yyyy-mm-dd HH:MM:SS EST and replace to it if dosent match that format . Any idea how to start with.
# 2  
Old 10-17-2013
So long as you have some consistency in the date format something like the following (adapted to your set of date string types)
Code:
#!/usr/bin/perl

use strict;
use warnings;

my %MON=qw(JAN 1 FEB 2 MAR 3 APR 4 MAY 5 JUN 6 JUL 7 AUG 8 SEP 9 OCT 10 NOV 11 DEC 12);
sub iso_date{
        my $date=shift;
        #Oracle style date
        $date=~s/(\d{2})-([A-Z]{3})-(\d{2})/sprintf "20%02d-%02d-%02d",$3, $MON{$2},$1/eg;
        #dd-mm-yyyy
        $date=~s/(\d{2})-(\d{2})-(\d{4})/$3-$2-$1/;
        #midnight datestamp
        $date=~s/(\d{4})(\d{2})(\d{2})0*/$1-$2-$3/;
        #USian mm/dd/yy
        $date=~s/(\d{2})\/(\d{2})\/(\d{2})/20$3-$2-$1/;
        return $date;
}
open (my $in_file , '<', $ARGV[0]);
open (my $new_file,'>',"$ARGV[0].tmp");
while (<$in_file>){
        my @rec=split(/\|/,$_);
        @date_fields=qw(0 11 24);
        for my $date_field ((@date_fields)){
                $rec[$date_field] = iso_date($rec[$date_field]);
        }
        print $new_file (join"\t",@rec);
}
rename("$ARGV[0].tmp","$ARGV[0]");

This User Gave Thanks to Skrynesaver For This Post:
# 3  
Old 10-17-2013
If you actually have multiple formats like:

yyyy/mm/dd
Wed 9 Sep 2009

And so on, then cleaning them up can be a nightmare. Try reading the tsv into Excel, and then have Excel produce a fixed format. First. This will also find bogus dates.
This User Gave Thanks to jim mcnamara For This Post:
 
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 compare two dates in a specific format ?

Hello, I am not able to find how to compare two dates in '%Y-%m-%d %H:%M:%S,%3N' format i have to two dates as below date1="2016-08-24 09:47:40,444" date2="2016-08-24 10:45:40,567" how to compare these two dates ? I have tried below without success if ; then echo 'yes'; fi ... (6 Replies)
Discussion started by: Ramneekgupta91
6 Replies

2. Shell Programming and Scripting

Python script to convert date to iso format

Hi , This is my 1st program in python never tried any python before. i am trying to write a python script which reads a .tsv file line by line and in each line it should look for mm/dd/yyyy formate and convert it to yyyy-mm-dd formate . can some one provide be some sample code to do that. (2 Replies)
Discussion started by: vikatakavi
2 Replies

3. Shell Programming and Scripting

Converting windows format file to unix format using script

Hi, I am having couple of files which i used to copy from windows to Linux, so now in case of text files (CTRL^M) appears at end of line. I know i can convert this windows format file to unix format file by running dos2unix. My requirement here is that i want to do it automatically using a... (5 Replies)
Discussion started by: sarbjit
5 Replies

4. Solaris

Date after 5 dates in YYYYMMDD format

Hi Experts, How to get date 5 days after current date in YYYYMMDD format? How do we compare date in YYYYMMDD format? Thanks (1 Reply)
Discussion started by: needyourhelp10
1 Replies

5. Shell Programming and Scripting

Dates not comparing correct even the same format

I have the date of the file passed into a variable also current date formatted same passed into a separate variable and compare the two with an if statement and statement always comes up false. Even though I verified the dates. Any help would be awesome. Filecrtdate=`ls -l $i | awk '{print... (19 Replies)
Discussion started by: coderanger
19 Replies

6. Shell Programming and Scripting

Need script to generate all the dates in DDMMYY format between 2 dates

Hello friends, I am looking for a script or method that can display all the dates between any 2 given dates. Input: Date 1 290109 Date 2 010209 Output: 300109 310109 Please help me. Thanks. :):confused: (2 Replies)
Discussion started by: frozensmilz
2 Replies

7. UNIX for Advanced & Expert Users

Select entries between two dates by converting Unix timestamp in Oracle Database.

Hi, I need to select the entries between two dates from an Oracle db. The Oracle db has a column with Unix timestamps. I use the following querry, but it doesnt seem to be working as desired. select count(*) from reporter_status where to_char(FIRSTOCCURRENCE, 'mm-dd-yy') between ('08-07-06')... (1 Reply)
Discussion started by: amitsayshii
1 Replies

8. UNIX for Dummies Questions & Answers

converting 6 digit column to dates format

Hi everyone, I have problem where I need to convert numbers to date format and then figure out what is less than or equal to specific date, the data comes out of a report in the following format 071205 141005 091205 111105 051005 141005 261005 181005 so I need to firstly run through... (15 Replies)
Discussion started by: Gerry405
15 Replies

9. Shell Programming and Scripting

ls -l output with long iso format

Hello everbody, I've a list of logfiles (transfer1.log, transfer2.log, ... transfer168.log.) Each of these was created at the end of the actual month and I've to write a script to calculate the utilisation ratio of the actual month of the system. I don't want to scan every logfile, so I've to... (1 Reply)
Discussion started by: rofflox
1 Replies

10. Shell Programming and Scripting

Format dates

I have a date that is received as text in Jan 1 2002 12:00AM format. Can anyone give me any ideas how to format that in oracle format i.e. 01-JAN-02 (1 Reply)
Discussion started by: jinky
1 Replies
Login or Register to Ask a Question