Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
Old 10-24-2008, 05:04 PM   #1
peterv6
Prospect
 
Join Date: Oct 2008
Location: Leominster, Massachusetts
Posts: 48
sed with -i switch not working in OSX Leopard

I'm new to OSX Leopard and Macs in general, but I've used sed for years. I'm trying to run the following sed script:

sed -i 's/^[[:space:]]\{11\}//g' menu1a.sh

This is the error message I get.
sed: 1: "menu1a.sh": invalid command code m

If I run this script without the -i switch, the script works as expected.

Also, if I run it in an ssh window in Linux, the -i switch version also works.

Does anyone have any ideas on what the problem is? Any advice would be greatly appreciated.

peterv6
peterv6 is offline   Reply With Quote
Old 10-24-2008, 05:59 PM   #2
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,409
The sed manpage on Leopard has the following synopsis:
Quote:
Originally Posted by man sed
sed [-Ealn] command [file ...]
sed [-Ealn] [-e command] [-f command_file] [-i extension] [file ...]

which seems to indicate that if you want to use the "-i" option, you need to supply the command using the "-e" option.

Sed, like many other Unix tools is not necessarily exactly the same across different versions of Unix.
The Leopard version of all Unix tools are supposed to adhere to the Open Brand UNIX 03 specifications (SUSv3 and POSIX 1003.1). It is quite likely that the previous versions of Unix you have used did not conform to this standard.
__________________
hayne.net/macosx.html

Last edited by hayne; 10-24-2008 at 06:02 PM.
hayne is offline   Reply With Quote
Old 10-24-2008, 06:12 PM   #3
baf
MVP
 
Join Date: Jun 2007
Location: Skellefteċ, Sweden
Posts: 1,173
You probably need:
sed -i "" '/...../' filename

see this post
__________________
/Bengt-Arne Fjellner IT-Administrator Luleċ university, Sweden.
Some say: "You learn as long as you live".
My way: "You live as long as you learn".
baf is offline   Reply With Quote
Old 10-24-2008, 07:11 PM   #4
Hal Itosis
Hall of Famer
 
Join Date: Apr 2002
Posts: 3,315
Quote:
Originally Posted by peterv6
I'm new to OSX Leopard and Macs in general, but I've used sed for years. I'm trying to run the following sed script:

sed -i 's/^[[:space:]]\{11\}//g' menu1a.sh

This is the error message I get.
sed: 1: "menu1a.sh": invalid command code m
If I run this script without the -i switch, the script works as expected.
Also, if I run it in an ssh window in Linux, the -i switch version also works.

Seems like Linux is being far too generous.

-i 's/^[[:space:]]\{11\}//g'
<-- makes it look like
"s/^[[:space:]]\{11\}//g" is the extension given to "-i"

baf's quotes serve to clarify to the interpreter (and to humans reading it) that
there is no extension. hayne's -e is also a means with which to disambiguate.

Thus, these all work:
sed -i "" 's/^[[:space:]]\{11\}//g' menu1a.sh
sed -i -e 's/^[[:space:]]\{11\}//g' menu1a.sh
sed -ie 's/^[[:space:]]\{11\}//g' menu1a.sh
sed -i "" -e 's/^[[:space:]]\{11\}//g' menu1a.sh


Makes sense.
Hal Itosis is offline   Reply With Quote
Old 10-26-2008, 07:38 PM   #5
peterv6
Prospect
 
Join Date: Oct 2008
Location: Leominster, Massachusetts
Posts: 48
Hal, Hayne, & Baf, thank you all. I worked in a Linux shop (Ubuntu), which as Hal noted must be a little "generous".

Also a big Thanks! to BAF for including his link to the post explaining the differences in sed between systems. I'll remember to check the man pages from now on!

jt6
peterv6 is offline   Reply With Quote
Old 10-26-2008, 07:45 PM   #6
peterv6
Prospect
 
Join Date: Oct 2008
Location: Leominster, Massachusetts
Posts: 48
BAF, one question, when you say system sed manual, what exactly are you referring to? I know what man pages are, but "system sed manual" I'm unfamiliar with.
Thanks again....
peterv6 is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 07:28 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.