Help with MySQL (.sql) and Shell Script
Question
How do I call the following in a .sql file?
mysql -u username -h serverip db -e "select a, b, c, d from mytable" > '/home/dump/result.txt';
I will then have to convert the tab separated file to csv for which I want to use sed.
- Is there a way to do it all in one line? (append sed command to convert the .txt file to csv)
- How to use an os.system call in .sql file? Will it work?
Solution
Try this:
mysql --batch --skip-column-names -u username -h serverip db \
-e "select a, b, c, d from mytable" \
| sed -e 's/\t/,/g' \
> '/home/dump/result.txt'
See docs for --batch
and --skip-column-names
for more info.
OTHER TIPS
What about to run this query via mysql
console client:
SELECT ... FROM ... INTO OUTFILE '/path/to/file.csv'
FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY "\n"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow