0

I am trying the below code to replace the string /IRM/I with E/IRM/I but am getting the file processed with no error and no transformation. I assume I'm using the cancel character incorrectly to allow the forward slash. Any help is much appreciated.

sed -i '/\/IRM\/IE\/IRM\/I/g'
  • BTW, if you aren't passing a filename, you shouldn't be using the `-i` argument to `sed`. – Charles Duffy Jan 10 '20 at 21:53
  • sorry, I am passing a filename, I just left it off the code. I am also changing other strings successfully but non contain a forward slash like this last one – Skonectthedots Jan 10 '20 at 21:54
  • BTW, if you *do* want to keep `/` as your sigil, you need to add escapes only before the forward slashes that should be treated as literal data, and leave it out otherwise. So that would be `sed -e 's/\/IRM\/I/E\/IRM\/I/g'` -- but it's much cleaner and more readable to just pick a different sigil entirely, or for that matter a tool other than `sed`. – Charles Duffy Jan 10 '20 at 22:01

1 Answers1

0

A sed command needs to specify an operation (like s to replace), and that operation requires a sigil. You don't need to use a slash as that sigil.

printf '%s\n' 'This is a test: </IRM/I>' | \
  sed -e 's@/IRM/I@E/IRM/I@g'

...correctly emits as output:

This is a test: <E/IRM/I>

Note that we added a s at the beginning of your sed expression, and followed it up with a @ -- a sigil that isn't contained anywhere in the source or replacement strings, so you don't need to escape it as you would /.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441