1

* NOT A DUPLICATE BECAUSE I WANT TO USE SED. *

I'm trying write a script that used sed on strings containing characters like (, [, /, \, * and spaces

I was having trouble with grep until I found the option -F: "Interpret pattern as a set of fixed strings"

How can I make sed behave the same way? I can't find any useful options.

The script is called with the string to be replaced, e.g. scriptname '\/-2])*' 'cat' means I want \/-2])* to be replaced with cat

In my script I set the strings as variables:

from="${1}"

to="${2}"

I fixed it to sed "s/'"$from"'/'"$to"'/g" and am trying to run scriptname 'dog c' 'cat' but it says sed: 1: "c'/'cat'/g": command c expects \ followed by text

Ive tried so many things and I don't know how to make this work!

user
  • 352
  • 3
  • 13
  • To fix the "unexpected EOF" error, use `sed 's/'"$from"'/'"$to"'/g'` -- pay attention at the order of single and double quotes! – Andrea Corbellini Jan 22 '16 at 18:34
  • ...or just use `sed "s/$from/$to/g"`, which is simpler – Andrea Corbellini Jan 22 '16 at 18:35
  • @AndreaCorbellini if that worked, I wouldn't be asking this question lol. Maybe you should try it yourself. – user Jan 22 '16 at 18:36
  • I know it doesn't solve your problem (otherwise I would have written an answer), but it fixes the "unexpected EOF" error – Andrea Corbellini Jan 22 '16 at 18:37
  • I fixed it to `sed "s/'"$from"'/'"$to"'/g"` and am trying to run `scriptname 'dog c' 'cat'` but it says `sed: 1: "c'/'cat'/g": command c expects \ followed by text` – user Jan 22 '16 at 18:39
  • Oh... I think you are confused about how to use `sed` and regular expressions: you don't need single quotes around arguments, and you should properly escape special characters. But again, fixing these errors won't solve your problem. – Andrea Corbellini Jan 22 '16 at 18:42
  • I saw it as a solution in a similar question so that's why I tried it. I can't manually escape special characters because the strings will be different each time. That's why I'm looking for something that will do it for me within the script. What will solve my problem? – user Jan 22 '16 at 18:44
  • Re-reading your question, I think I misunderstood. What I said will actually solve your problem. Use `from="$(sed -e 's/[]\/$*.^|[]/\\&/g' <<< "$1")"`, `to="$(sed -e 's/[\/&]/\\&/g' <<< "$2")"` and `sed -e "s/$from/$to/g"` – Andrea Corbellini Jan 22 '16 at 18:57
  • Just escape your arguments. For a \, \\\ works. For a (, `(` works. For a [. `\[` works. For a /, `/` works. For a *, `*` works. Curse this formatting. – Lando Jan 22 '16 at 19:00
  • Also see this question: http://stackoverflow.com/questions/407523/escape-a-string-for-a-sed-replace-pattern – Prune Jan 22 '16 at 19:08
  • @AndreaCorbellini yaaay! it works. The only problem is that I'm not sure how to adapt this to grep. Using -F doesn't work, using egrep doesn't work, using just double quotes like for sed doesn't work. – user Jan 22 '16 at 19:38
  • NVM I figured it out. THANK YOU! – user Jan 22 '16 at 19:48

0 Answers0