'sed -i' syntax not working for Terminal

Joined
Nov 4, 2011
Messages
4
Reaction score
0
Points
1
Hi,

I have a question on Terminal 2.2.1.

I have a text file, tmp.txt, with these contents:
abc
blah
efg

I am trying to understand why the following command doesn't work (to replace 'blah' with 'the'). It works fine in both bash and tcsh in linux. Perhaps there is something different going on with quotes in Terminal?

>$ sed -i '2s/blah/the/' tmp.txt
sed: 1: "tmp.txt": undefined label 'mp.txt'

Does anyone know alternative syntax to make this work in Mac Terminal?

Any help would be greatly appreciated!,
Will

P.S. This works fine, but I want to use 'sed -i' to make the change:
>$ sed '2s/blah/the/' < tmp.txt
abc
the
efg
 
Joined
Nov 5, 2011
Messages
1
Reaction score
0
Points
1
The version of sed included in Mac OS X requires an argument for -i command line option. The argument is the extension for backup file. So you need to use

sed -i '.bak' '2s/blah/the/' tmp.txt

to edit tmp.txt in-place and storing backup in tmp.txt.bak. If you do not want backup it is

sed -i '' '2s/blah/the/' tmp.txt

HTH,
Wojtek
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top