[YLUG] How do to substition in a file with sed or similar

Gavin Atkinson gavin.atkinson at ury.york.ac.uk
Fri Oct 27 12:17:17 BST 2006


On Fri, 2006-10-27 at 11:47 +0100, Robert Hulme wrote:
> Hi list,
> 
> I know I can:
> sed s/foo/bar/ myfile
> 
> which dumps to stdout
> 
> How I do the substition on the file 'in place'?
> 
> sed s/foo/bar/ myfile > myfile
> 
> Results in a zero length file (which I'd kinda expect from the stream editor).
> 
> Do I need to use a temporary file or is there some kind of buffer I could use:
> sed s/foo/bar/ myfile | streambuffer > myfile
> 
> Or is there a magic switch or should I be using something other than sed?

Many (although not all sed's) have an -i switch, for editing in place.
It's not standard, so don't use it if you want your scripts to work
everywhere.

Gavin



More information about the York mailing list