MySQL 查询将输出作为 CSV 打印到标准输出

发布于 2024-09-27 12:59:30 字数 407 浏览 6 评论 0原文

我想要执行以下操作 mysql -uuser -ppass -h remote.host.tld 数据库 script.sql

其中 script.sql 包含以下内容

SELECT *
FROM webrecord_wr25mfz_20101011_175524
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' 
LINES TERMINATED BY '\n'

我希望 CSV 输出定向到标准输出。原因是因为使用 INTO OUTFILE 'blah.csv' 运行此查询会将文件保存在远程主机上。我希望将文件保存在本地主机上。

如果我可以将标准输出重定向到一个文件,那就太棒了。

I want to do the following mysql -uuser -ppass -h remote.host.tld database < script.sql

where script.sql contains the following

SELECT *
FROM webrecord_wr25mfz_20101011_175524
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' 
LINES TERMINATED BY '\n'

I want CSV output directed to standard out. The reason is because running this query with an INTO OUTFILE 'blah.csv' will save the file on the remote host. I want the file saved on the local host.

If I could just redirect the standard output to a file, that would be dandy.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮云落日 2024-10-04 12:59:30

上面的答案似乎并没有完全回答原来的问题,我不确定这是否可以,但希望这可以帮助某人:

请参阅如何以 CSV 格式输出 MySQL 查询结果? 有关如何 sed 的大量评论。例如,根据原始参数,以下内容可能就足够了:

mysql --batch -u user -h remote.host.tld database --port 3306 -ppass -e "SELECT * FROM webrecord_wr25mfz_20101011_175524;" | sed 's/\t/,/g' 2>&1

这与上面的答案类似,但重定向到 stdout 而不是 blah.csv

但是,(虽然不确定如果您需要保留选项卡这是否有效,但有很多方法可以解决这个问题),我使用了 https://stackoverflow.com/a/2543226/2178980 要正确转义双引号并转换为逗号分隔:

mysql --batch -u user -h remote.host.tld database --port 3306 -ppass -e "SELECT * FROM webrecord_wr25mfz_20101011_175524;" | perl -lpe 's/"/\\"/g; s/^|$/"/g; s/\t/","/g' 2>&1
  • 执行 sql "SELECT * FROM webrecord_wr25mfz_20101011_175524;" 通过 mysql (此输出将以制表符分隔)
  • 通过管道传输到 perl -lpe 's/"/\\"/g; 转换为逗号分隔; s/^|$/"/g; s/\t/","/g'
  • 通过附加 2>&1 将输出转到 stdout代码>

The answers above don't seem to fully answer the original question, and I'm not sure if this does either, but hopefully this might help someone:

See How to output MySQL query results in CSV format? for a lot of comments regarding how to sed. For example, based on the original parameters, the following might be sufficient:

mysql --batch -u user -h remote.host.tld database --port 3306 -ppass -e "SELECT * FROM webrecord_wr25mfz_20101011_175524;" | sed 's/\t/,/g' 2>&1

This is similar to the answer above, but redirecting to stdout instead of blah.csv.

However, (although not sure if this will work if you need to preserve tabs, there are many ways to address this though), I've used https://stackoverflow.com/a/2543226/2178980 to correctly escape double quotations and convert to comma-separated:

mysql --batch -u user -h remote.host.tld database --port 3306 -ppass -e "SELECT * FROM webrecord_wr25mfz_20101011_175524;" | perl -lpe 's/"/\\"/g; s/^|$/"/g; s/\t/","/g' 2>&1
  • Execute the sql "SELECT * FROM webrecord_wr25mfz_20101011_175524;" via mysql (this output will be tab-separated)
  • Convert to comma-separated by piping to perl -lpe 's/"/\\"/g; s/^|$/"/g; s/\t/","/g'
  • Have the output go to stdout by appending 2>&1
醉生梦死 2024-10-04 12:59:30

部分是对 MySQL SELECT INTO OUTFILE to a different server 的重复问题。如果没有 into outfile 则不能使用 FIELDS TERMINATED BY

一个(不太优雅的)替代方案是使用 --batch 选项用于生成制表符分隔的输出并插入标准输出。像这样的事情:

mysql --batch -uuser -ppass -h remote.host.tld database < stack.sql | sed 's/\t/,/g' > blah.csv

请注意 --batch 转义特殊字符,因此根据您拥有的数据及其可预测性,您可能需要更改 sed

In part a duplicate question to MySQL SELECT INTO OUTFILE to a different server. FIELDS TERMINATED BY can't be used without into outfile

A (not so elegant) alternative is using the --batch option to produce tab separated output and sedding the stdout. Something like this:

mysql --batch -uuser -ppass -h remote.host.tld database < stack.sql | sed 's/\t/,/g' > blah.csv

Be aware that --batch escapes special characters so depending on the data you have and its predictability, you might need to change the sed

国粹 2024-10-04 12:59:30

试试这个: mysql -uuser -ppass -h remote.host.tld 数据库 脚本.sql 2> blah.csv

这将重定向 stderr

Try this: mysql -uuser -ppass -h remote.host.tld database < script.sql 2> blah.csv

This will redirect the stderr

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文