Changing files on the fly in Linux (writing to input file on a pipe)

Try using sponge from moreutils like this:

sed "s/root/toor/" /etc/passwd | grep -v joey | sponge /etc/passwd

It collects the whole input before writing to it's output.


I would guess sed still might create the temp file, but the following might do what you want? (Using strace on this might show you if sed creates a temp file or not).

sed -i '/bar/!d' foo.txt

The exclamation inverts the match, d is for delete, so this removes all lines that don't have bar in them.

Tags:

Linux

Pipe