开发者

Shell script: send sed output to mysql?

Trying to pipe the outpu开发者_运维百科t of a sed replacement on a text file into MySQL like so:

mysql -D WAR | sed -e "s/2000/$START/g" -e "s/2009/$END/g" < WAR.sql

That's not working. Nor is:

mysql -D WAR < sed -e "s/2000/$START/g" -e "s/2009/$END/g" < WAR.sql

What's the proper solution here?


sed "s/2000/$START/g;s/2009/$END/g" WAR.sql | mysql -D WAR


sed -e "s/2000/$START/g" -e "s/2009/$END/g" < WAR.sql | mysql -D WAR


Just to explain a bit more, the command line runs from left-to-right, except in the case of redirection of input. So, the reason your first one didn't work is because that was taking the OUTPUT of mysql and sending it to sed, as well as trying to redirect input to sed from file 'WAR.sql'.

In the second case, you're using file redirection on mysql, except you're giving it a command (sed, in this case), which meant that the shell probably tried to open a [non-existent] file named 'sed' in the current directory. The next input redirection probably just confused it if the shell even got that far in parsing.

I believe (and voted for) the answer from gbacon as the simplest solution to achieve what I think you want.


mysql -D WAR < <(sed -e "s/2000/$START/g" -e "s/2009/$END/g" < WAR.sql)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜