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

Tags used in this thread
Popular LQ Tags , ,

Reply
 
Thread Tools
Old 06-05-2009, 04:38 PM   #1
srunni
LQ Newbie
 
Registered: Jun 2007
Posts: 24
Thanked: 0
sed error "command c expects \ followed by text" under OS X (but works in Linux)


[Log in to get rid of this advertisement]
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!
srunni is offline  
Tag This Post , ,
Reply With Quote
Old 06-05-2009, 05:25 PM   #2
David the H.
Senior Member
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian Sid/kde3.5
Posts: 2,464
Thanked: 130
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.
David the H. is offline     Reply With Quote
Old 06-05-2009, 05:53 PM   #3
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,969
Thanked: 19
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).
rjlee is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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-06-2009 01:44 PM
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 01:36 PM.

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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration