0

I want to replace "1/1" with "1/2" in all files in a directory

I tried

find . -type f -exec sed -i 's/1/1/1/2/g' {} +

and got

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

what am i doing wrong? how to use this when i need replace it with pattern containing "/"?

Thanks in advance

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • https://www.gnu.org/software/sed/manual/sed.html#The-_0022s_0022-Command – melpomene Nov 26 '17 at 20:28
  • Either `sed -e 's/1\/1/1\/2/g'` (quote the slash) or `sed -e 's@1/1@1/2@g'` (use a different separator), whichever method you like better. – AlexP Nov 26 '17 at 20:30

1 Answers1

0

The substitute(s) command have forward slashes that delimits it. Its format is :

s/pattern/replacement/flag

The real question here is how you would tackle the forward slashes which appear in your pattern. You can escape the forward slashes in your pattern which is the dirty way to do it.

Fortunately sed allows different delims for s command So you could do something like

find . -type f -exec sed -i 's#1/1#1/2#g' {} \;

Here note the use of # as the delim for s which helps you avoid an influx of backslashes in your substitution.

sjsam
  • 21,411
  • 5
  • 55
  • 102