2

I'd like to replace all the \r\n with < br/ >in a document, and I'm trying this see script below

# sed -i 's/\r\n/<br/>' ~/xxd/*

however i got this error back

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

How do i solve this problem?

Thanks!

  • possible duplicate of [Sed - unknown option to \`s'](http://stackoverflow.com/questions/9366816/sed-unknown-option-to-s) – tripleee Aug 10 '14 at 08:30

1 Answers1

3

Your problem is that you have the / separator in your replacement string so sed is assuming that's the end of your replacement, and that the > following it is a flag.

If your sed is modern enough, just use a different separator character, one that's not in the replacement string:

pax$ echo hello | sed -e 's/e/<br />/'
sed: -e expression #1, char 9: unknown option to `s'

pax$ echo hello | sed -e 's?e?<br />?'
h<br />llo

Alternatively, you can escape the offending character but I try to avoid that since it tends to lead to overly sawtooth sed commands like /\/\/\/\/\/\.

The other thing you may want to watch out for is trying to use \n in your regex since sed operates on lines anyway. If your intent is to just strip carriage returns and insert HTML line breaks, then the following sed command may be better:

s?\r$?<br />?
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Thanks for your reply. However the output seems wrong # echo test\r\ntest | sed -e 's?\r$?
    ?' testrntest
    –  Nov 02 '11 at 08:18
  • 1
    @xxd: that's because the _shell_ is interpreting those escapes, as can be seen if you just use `echo test\r\ntest` on its own. If you don't want the shell munging them, you need to single-quote the argument. You'll also probably need `echo -e` to interpret the escapes rather than pass them through as the characters `backslash`, `r`, `backslash`, `n`. – paxdiablo Nov 02 '11 at 08:27
  • Not sure what I'm doing wrong but I can't seem to make it work from my end. Tried testing it in cygwin and RHEL5 but cant' make it work. However, I found an alternative: `sed 's|$|
    |g' file.html`. I figured that I would just replace the end character with `
    ` and indeed it worked!
    – icasimpan Oct 22 '14 at 07:31