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

linuxpipe

How to change a file on the fly along a pipe?

I'm probably looking for a way to buffer a file at the beginning of a pipe, which in contrast to this:

    cat foo.txt | grep bar > foo.txt

… would preserve the input data from destruction by the pipe itself. Is there such a buffer in stock?

Best Answer

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.

Related Topic