I have a template file like this
$ cat template.txt
QWERTY
~SQL~
ASDFG
I need to substitute the "~SQL~"
string in the template file with following text. This text is stored in a variable
SELECT COL1,
COL2,
COL3
FROM TABLE;
I tried the following code, but got an error: sed: -e expression #1, char 20: unterminated `s' command
$ query='SELECT COL1,
> COL2,
> COL3
> FROM TABLE;'
$
$ sed "s/~SQL~/$query/" template.txt
sed: -e expression #1, char 20: unterminated `s' command
If I remove the new lines from "query"
, the sed command works fine
$ query='SELECT COL1, COL2, COL3 FROM TABLE;'
$ sed "s/~SQL~/$query/" template.txt
QWERTY
SELECT COL1, COL2, COL3 FROM TABLE;
ASDFG
I would like to preserve the new lines and formatting while substituting the text. How do I achieve this?
You can use this awk:
awk -v q="$query" '/~SQL~/{$0 = q} 1' file
QWERTY
SELECT COL1,
COL2,
COL3,
FROM TABLE;
ASDFG
You can do this:
query='SELECT COL1,\n COL2,\n COL3\nFROM TABLE;'
sed "s/~SQL~/$query/" template.txt
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.