Avatar of skij
skijFlag for Canada

asked on 

MySQL: Run query from Linux command line and output results to file

I want the results of this query on my database called `mydatabase` to be written to a file named xyz.txt
SELECT * FROM Contacts LIMIT 10 >xyz.txt

Open in new window


How can I do this?  This does not work:
mysql -u myusername -pmypassword -h my.host.example.com -d mydatabase SELECT * FROM Contacts LIMIT 10 >xyz.txt

Open in new window

MySQL ServerLinux

Avatar of undefined
Last Comment
arnold
ASKER CERTIFIED SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of skij
skij
Flag of Canada image

ASKER

Thanks.  If I use echo, how should I escape the " and '?

For example:
SELECT concat('"' , REPLACE(REPLACE(`Email`, '"', ''), ',', '') , '","' , REPLACE(REPLACE(`FName`, '"', ''), ',', '') , '" ###') From Contacts WHERE Email <>'' LIMIT 10

Open in new window

This does not work:
echo "SELECT concat(\'\"\' , REPLACE(REPLACE(`Email`, \'\"\', \'\'), \',\', \'\') , \'\",\"\' , REPLACE(REPLACE(`FName`, \'\"\', \'\'), \',\', \'\') , \'\" ###\') From Contacts WHERE Email <>\'\' LIMIT 10" |mysql -u username --password=password -h hostname databasename >query_results.txt

Open in new window

Avatar of arnold
arnold
Flag of United States of America image

when you use " as the enclosing quotes for the echo, you would only need to escape the double quotes.
you have to also escape the exec (`) quotes



You seem to have an errand  \'" ###\' with the single quote mismatched.
echo the line out first and see how it looks.


Can you paste the pattern that you want to replace, the commas (,) if they are part of the pattern replacement need to be escaped as well.

replace(replace (\`Fname\`,'\"',''),',',''), '\" ###')

See if you can keep track of your open/close parenthesis.
you seem to have a select replace(replace()),'","' that does not seem to make sense

replace(replace(\`Email\`,'\"',''), ',' , '')
'\",\"'

See if you group the above
echo "select concat('\"',<replace Email>,'\",\"',<replacefname>) from Contacts where Email<>'' limit 10"
Linux
Linux

Linux is a UNIX-like open source operating system with hundreds of distinct distributions, including: Fedora, openSUSE, Ubuntu, Debian, Slackware, Gentoo, CentOS, and Arch Linux. Linux is generally associated with web and database servers, but has become popular in many niche industries and applications.

71K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo