tags:

views:

96

answers:

4

Trying to pipe the output 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?

+3  A: 
sed -e "s/2000/$START/g" -e "s/2009/$END/g" < WAR.sql | mysql -D WAR
Greg Bacon
no need for the input redirection.
ghostdog74
A: 
mysql -D WAR < <(sed -e "s/2000/$START/g" -e "s/2009/$END/g" < WAR.sql)
Ignacio Vazquez-Abrams
+1  A: 

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.

Chris Cleeland
+2  A: 
sed "s/2000/$START/g;s/2009/$END/g" WAR.sql | mysql -D WAR
ghostdog74