Solved

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

Posted on 2011-02-25
6
624 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 500 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

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.…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

772 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