LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   sed error "command c expects \ followed by text" under OS X (but works in Linux) (http://www.linuxquestions.org/questions/programming-9/sed-error-command-c-expects-%5C-followed-by-text-under-os-x-but-works-in-linux-730997/)

srunni 06-05-2009 04:38 PM

sed error "command c expects \ followed by text" under OS X (but works in Linux)
 
Hi,

I'm trying to find a line containing the text "foo" and replace that whole line (i.e., the rest of the line as well) with the text "bar". Under Linux, I'm able to use sed like so:
Code:

sed -i'.bak' -e"/foo/ c\
bar
"

Under OS X, however, I get the error
Code:

sed: 1: "/foo/ cbar": command c expects \ followed by text
The Linux testing was done on Gentoo with sed 4.1.5, while the Mac OS is version 10.5.7 (not sure how to get the sed version, as it doesn't have a --version/-v flag or anything), if that's relevant.

Thanks!

David the H. 06-05-2009 05:25 PM

It looks like your backslash is getting gobbled up somehow, so that it disappears before getting passed to sed. Try enclosing your expression in single quotes, and/or escaping the backslash.

rjlee 06-05-2009 05:53 PM

Bash is the default shell on most versions of UNIX, including Mac OS X and many Linux distributions.

Bash treats a backslash at the end of the line as a line continuation character, even inside double-quoted strings. So when bash executes sed, it doesn't pass the newline character or the backslash.

You have two options. You can change your shell (use "echo $SHELL" under Linux to see which shell you are using) to make both environments the same, or you could change the way you quote the input. You could double up the backslash, which will cause the first backslash to escape the second rather than the newline, making it to pass a single backslash and the newline to the shell, or you could use single-quotes around the -e parameter to treat it as a literal string without any substitutions (including backslash escapes, backticks, variable substitution, and so on).

primorec 12-02-2009 03:52 AM

Quote:

Originally Posted by David the H. (Post 3564622)
It looks like your backslash is getting gobbled up somehow, so that it disappears before getting passed to sed. Try enclosing your expression in single quotes, and/or escaping the backslash.


GNU version of sed works fine

BSD implementation of sed has few not resolved issues


taken from Mac OSX man sed pages

A sed command, written by L. E. McMahon, appeared in Version 7 AT&T UNIX.

AUTHORS
Diomidis D. Spinellis <dds@FreeBSD.org>

BUGS
Multibyte characters containing a byte with value 0x5C (ASCII `\') may be incorrectly treated as line continuation characters
in arguments to the ``a'', ``c'' and ``i'' commands. Multibyte characters cannot be used as delimiters with the ``s'' and
``y'' commands.

BSD May 10, 2005 BSD


All times are GMT -5. The time now is 08:58 AM.