Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-02-25
6
Medium Priority
?
674 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
ID: 34984274
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
ID: 34984515
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 2000 total points
ID: 34984604
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:digitaly
ID: 34985214
Awesome!  Worked great!  Thanks a load!!! :-)
0
 

Author Closing Comment

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

Expert Comment

by:sjklein42
ID: 34985248
thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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…
Six Sigma Control Plans

963 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