LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-05-2009, 04:38 PM   #1
srunni
LQ Newbie
 
Registered: Jun 2007
Posts: 24

Rep: Reputation: 15
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!
 
Old 06-05-2009, 05:25 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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.
 
Old 06-05-2009, 05:53 PM   #3
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990

Rep: Reputation: 67
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).
 
Old 12-02-2009, 03:52 AM   #4
primorec
Member
 
Registered: Sep 2004
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: 62

Rep: Reputation: 15
Quote:
Originally Posted by David the H. View Post
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
 
  


Reply

Tags
mac, os x, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ns:"error when calling class OldSim"&tclsh:"invalid command+child process exits abn." shojaru Linux - Newbie 0 03-05-2009 05:23 AM
LFS6.3 livecd "ls : command not found" error after "su - lfs" rotu Linux From Scratch 2 06-19-2008 04:59 PM
"hosts allow" in smb.conf expects only FQDN, not alias or netbios name!? Not now, John! Linux - Networking 1 10-13-2006 06:17 AM
can I replace text with the result of "wc" using sed? BrianK Linux - General 1 04-21-2004 02:15 PM
"segmentation error" when issuing "useradd" command through terminal with RH 9.0 kaihuang Linux - General 0 10-22-2003 12:47 AM


All times are GMT -5. The time now is 06:27 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration