0

When I try following commands, (I'd like to rewrite sql).

Day='2020/12/1'
Dir=/home/test/data


sql=`cat $Dir"/"$test".sql" | sed -e "s/Day/$Day/g"`

I suffered following errors.

sed: -e expression #1, char 24: unknown option to `s'

Why the s is recognised as option ? why is this command couldnt work well ?

if someone has opinoin, please let me know

Thanks

1 Answer 1

4

If you use / as the separator for the s/pattern/replacement/flags sed command, then you need to escape the / in both pattern and replacement. Better to use a separator that doesn't occur in the pattern nor replacement:

Day='2020/12/1'
Dir=/home/test/data


sql=$(sed "s|Day|$Day|g" < "$Dir/$test.sql")

Since you're tagging zsh, you could also do:

sql=${"$(<$Dir/$test.sql)"//Day/$Day}

And then, you don't have to worry about this kind of issue nor about bytes not forming valid characters in the locale.

For multiple replacements:

typeset -A replace=(
  Day   "$Day"
  Month "$Month"
  Year  "$Year"
  '*'   Star
)
pattern="(${(j[|])${(kb@)replace}})"
set -o extendedglob
sql=${"$(<$Dir/$test.sql)"//(#m)$~pattern/$replace[$MATCH]}
2
  • Thank you for answering. I can do it . I now search some method which can substitute multiple strings ... thanks
    – Heisenberg
    Commented Jan 19, 2021 at 7:50
  • @Heisenberg, see edit to replace a list of strings with the zsh syntax. Commented Jan 20, 2021 at 13:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .