简体   繁体   中英

How to get get Output of SQL query in a flat file using shell script

我想使用外壳程序脚本自动执行一个包含多个SQL查询的SQL文件,同时我想将该SQl文件中最后一个查询输出转换为任何平面文件。

This awk command will find the last semicolon-separated query in the file and pipe it to mysql

awk 'BEGIN {RS=";"} NF > 0 {query=$0; } END {print query}' file.sql | mysql -u username -p password > output.txt

The NF > 0 keeps it from setting query to the empty line after the last ; .

You can create a file that executes each query but only sends the output of the last one.

#!/bin/bash

mysql -h... -u... -p... -e 'query1' > /dev/null

mysql -h... -u... -p... -e 'query2' > /dev/null

mysql -h... -u... -p... -e 'query3' > /result.sql

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM