Solved

How can I convert m/d/yyyy to YYYY-MM-DD in a CSV file?

Posted on 2011-02-25
6
614 Views
Last Modified: 2012-05-11
Hi EEs,
  I'm working on converting and cleansing a database dump from an older FileMaker Pro database to import into MySQL (for a web project) and need a nice tidy way to convert the date format of m/d/yyyy to YYYY-MM-DD.  I know perl, sed and awk should be able to do the trick, but what I've found so far hasn't worked (definitely a syntax problem).  It'd be great if your solution was a perl script that could be run as "date-fmt.pl FILENAME", but even a simple sed command would be appreciated.  I'm dealing with a database that has over 230,000 entries and using OpenOffice extensions to change the format takes wayyyyy too long.

0
Comment
Question by:digitaly
  • 3
  • 3
6 Comments
 

Author Comment

by:digitaly
Comment Utility
While I'm at it, if you can add the process in this sed command to the script, too, that'd be excellent.  No sweat if you can't or don't though.



sed -e 's/\"//g' -e 's/\\//g' -e 's/^/\"/' -e 's/$/\"/' -e 's/\t/\"\,\"/g'
0
 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
perl script

while ( <> )
{
	# m/d/yyyy
	# YYYY-MM-DD
	while (/([0-9]+)\/([0-9]+)\/([0-9]+)/)
	{
	    $_ = $` . $3 . '-' . sprintf("%02d",$1) . '-' . sprintf("%02d",$2) . $';
	}

	print $_;
}

Open in new window

0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
Comment Utility
Including your sed commands:

while ( <> )
{
	# m/d/yyyy
	# YYYY-MM-DD
	while (/([0-9]+)\/([0-9]+)\/([0-9]+)/)
	{
	    $_ = $` . $3 . '-' . sprintf("%02d",$1) . '-' . sprintf("%02d",$2) . $';
	}

	s/\"//g;
	s/\\//g;
	s/^/\"/;
	s/$/\"/;
	s/\t/\"\,\"/g;
	
	print $_;
}

Open in new window

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:digitaly
Comment Utility
Awesome!  Worked great!  Thanks a load!!! :-)
0
 

Author Closing Comment

by:digitaly
Comment Utility
Excellent, accurate and FAST response.  THANK YOU!!!
0
 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
thanks
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now