Search
  • Transform UK-format dates (dd/mm/yy) into US-format (mm-dd-yy)

    Date: Apr 7, 2009  |  Categories: Perl  |  Written By: Mike Ballan  

Suppose you have a text file containing lots of dates on separate lines and you want to change the date format:

For example, transform these UK-format dates (dd/mm/yy) into US-format (mm-dd-yy)

Contents of dates.txt:
28/07/99 – first date
13/06/00 – second date
06/07/01 – third date
08/12/02 – fourth date

A quick command line perl script can perform the transformation automatically for every date in the file whilst retaining the remaining contents of each line:

perl -ne ‘print “$2-$1-$3$4\n” if $_ =~ /([0-9]{2})\/([0-9]{2})\/([0-9]{2})(.*)/’ dates.txt > mod_dates.txt

Contents of mod_dates.txt:
07-28-99 – first date
06-13-00 – second date
07-06-01 – third date
12-08-02 – fourth date

Thanks goes to Fred..!



Leave a Reply

Back to Top