Q:

SED command for completing SQL query file

I've got a SQL query file to be filled with a variable (domainname) and two date values (start and end date). 
The idea is to bash script the SED command to create SQL query files per month and domain. 
Is it smart to search and replace the SQL query file with SED? 
When I try so, I need to escape the characters from the date value. 
The part of the SQL file looks as follows:
date < ''
AND
date >= ''
AND
to_domain = ''

The input of an example:

to_domain = testing.com date = 2017-04-01 date = 2017-04-02

Any ideas to do this smart? 
I'm sorry but I don't have enough experience in SED. 
I've Google'd my ass of with this one, but no luck so far. 
Thanks in advance! 

A:

You can use environment variables in a template query file and generate the new file with envsubst to substitute env vars. 
Given the following query-template.txt :
date < '$start_date'
AND
date >= '$end_date'
AND
to_domain = '$to_domain'

You can generate generate query.txt with envsubst and previously define your variables :
export start_date=2017-04-01
export end_date=2017-04-02
export to_domain=testing.com
envsubst < query-template.txt > query.txt

Or put all the variables export in a file vars.sh and source it before performing the substitution :
source vars.sh
envsubst < query-template.txt > query.txt

