sed error "command c expects \ followed by text" under OS X (but works in Linux)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
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.
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).
Distribution: RH5.2, RH6.2, RH8.0, RH 9.0, RHEL 3.0,MDK 10.1, KNOPPIX3.6, Solaris 8, Solaris 9, CentOS 3x-4x-5x, U
Posts: 57
Rep:
Quote:
Originally Posted by David the H.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.