I am doing some find/replace thing with sed and using tee to write output file.Here is the command
# $1 source
# $2 Type
# $3 name
# $4 body
sudo sed "s/<!--ID-->/1/g" ./templates/tpl.txt \
| sed "s/<!--AUTHOR-->/myname/g" \
| sed "s/<!--TYPE-->/$2/g" \
| sed "s/<!--BODY-->/$4/g" \
| sed "s/<!--NAME-->/$3/g" \
| tee "$3.txt" > /dev/null
In the output file I see “n” in place of new lines . I need the same effect of as of the following
(but after template substitution )
echo -e "$4" > "$3.txt"
I am bash learner and please help me furnish my code
Edit
$4 contains multiline string (e.g a mysql function /procedure or trigger ) with comments etc
thanks
If you plan to use
teefor writting privileged files, you have to usesudowithtee.You could:
other way: in bash ,you could use:
so:
could work too.
With Mac’s
sedthis may work better:Explanation:
${4//\\n/\\$'\n'}are bashism, we could use as you use bash. Mac sed don’t support\nas newline, but support a newline if escaped by a backslash\, so in RHS,\ncould be written (in bash):\\$'\n':