User request ~ we need some values that are stored in MySQL reported to us on a daily basis.
MySQL dumps data out in tab delimited format but the user needed it as a CSV and wasn't savvy enough to do the conversion themselves. Below is a simple bash script that runs the query contained in the daily numbers SQL file and pipes the results through sed and converts tabs to commas. Finally, it writes the results to a new file with the format of MarketingDailyNumbers.YYYY-mm-dd.csv
Caveats
As with all the code you find here, trust but verify. In particular, this script will not produced the expected CSV results if there are embedded commas or tabs in the data. It would need to wrap fields with the delimiter and escape unescaped values. I didn't have to spend the time working on those problems as my extract is purely numeric but this should be enough code for me to remember the syntax of various things.
Code
#!/bin/bash
# The sed replacement is the tab character
# Generated with ctrl-V tab
mysql -D MutualFundDB -uwebUser -pwebPwd < ./MarketingDailyNumbers.sql | sed 's/ /,/g' > MarketingDailyNumbers.`date "+%F"`
2 comments:
Curious why you decided to go that route rather than modify the MySQL query to just output as a CSV?
Thanks,
Jon
Ummm because I'm ignorant of how to do it within the query itself? Is that a parameter/switch when using MySQL? Or did you mean something like SELECT CONCAT(col1, ',', col2) AS output FROM MyTable
Post a Comment