Shell script: send sed output to mysql?

Trying to pass sed replacement output to a text file in MySQL as follows:

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

This does not work. Also no:

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

What is the right solution here?

+4
source share
4 answers
 sed -e "s/2000/$START/g" -e "s/2009/$END/g" < WAR.sql | mysql -D WAR 
+5
source
 sed "s/2000/$START/g;s/2009/$END/g" WAR.sql | mysql -D WAR 
+7
source

Just to explain a little more, the command line works from left to right, except when redirecting input. So, the reason your first one didn’t work is because it was taking OUTPUT mysql and sending it to sed, and also trying to redirect input to sed from the WAR.sql file.

In the second case, you use file redirection in mysql, except that you give it a command (sed in this case), which means that the shell probably tried to open a [nonexistent] file named 'sed' in the current directory. The following input redirection probably just confused him if the shell even got to the parsing.

I consider (and voted) the answer from gbacon as the simplest solution to achieve what I think you need.

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

Source: https://habr.com/ru/post/1299508/


All Articles