sed not working with -i?

I need to do a substitution on a bunch of files so I tried

find . -type f -exec sed -i 's/regex/replacement/g' {} \;

But I get this error

Code:
sed: 1: "piece of file name": invalid command code .

There are no complaints when I don't pass any option, but it doesn't replace anything in that case either. Anyone know how I can use sed to find replace or any other options?
 
I believe [red]-i[/red] should be followed with an extension name for the backup (I use a literal [red]""[/red] to not make a backup), so (and note well that I am not very good with find -exec, so do test safely please) I believe that what you want is [cmd=""]find . -type f -exec sed -i "" 's/bark/quack/g' {} \;[/cmd].

(I did just test it and it worked without a hitch here)
 
That's correct. In-place-editing requires a backup extension (like ".bak" or simply "" for none) to be passed on the command line. The syntax is [cmd=]sed -i extension[/cmd]. I'm not entirely convinced by fronclynn's suggestion, because it feels like file clobbering waiting to happen (saving output to a file named "something" while almost simultaneously keeping a backup in file "something" ...). Maybe some sed internal is preventing that, but it's a risk I wouldn't take.
 
True, but [cmd=""]sed 's/Biff/Marty/g' < filename > filename[/cmd] doesn't seem very cle^H^H^Hsane*, and with find, you could end up with thousands of backup files scattered around (though I suppose following up with [cmd=""]find . -type f -iname "*.bak" -delete[/cmd] would suffice.).



*and in fact, after testing it, just leaves a zero length file. Snort.
 
% sed -i "" is smart enough to understand in place edits. The example fronclynne cites doesn't work because the shell clobbers the file by truncating it due to stdout being redirected before the sed process gets a hold of any of the contents of the (now truncated) file.
 
Back
Top