Tag Archives: Unix

How to convert date format MM/DD/YYYY to YYYY/MM/DD using sed (Unix)

Damned Americans and their illogical data formats…trying to import a 100MB file into MySQL, and the date fields are in MM/DD/YYYY instead of the traditional YYYY/MM/DD that we all know and love…so I turned my hand to sed:

First, a quick test on the command line:

echo “MM/DD/YYYY” | sed -e “s_\(..\)\/\(..\)\/\(….\)_\3/\1/\2_”

=> YYYY/MM/DD

echo “textbeforeMM/DD/YYYYtextafter” | sed -e “s_\(..\)\/\(..\)\/\(….\)_\3/\1/\2_”

=>textbeforeYYYY/MM/DDtextafter

Then let’s let it loose on the file itself:

sed -e “s_\(..\)\/\(..\)\/\(….\)_\3/\1/\2_” test_in.csv > test_out.csv

Took a few seconds to run against a 100MB file 🙂

Many thanks to Bruce Barnett for his Sed – An Introduction and Tutorial , an invaluable reference for a neophyte like me 🙂