sponge

Read stdin and write to a file atomically without buffering

brewmacoslinux
Try with needOr install directly
Source

About

Soak up standard input and write to a file

Commands

sponge

Examples

Filter a file in-place using sed and sponge$ sed 's/old/new/g' file.txt | sponge file.txt
Process a file with grep and overwrite it safely$ grep 'pattern' data.txt | sponge data.txt
Redirect command output to the same file safely$ sort unsorted.txt | sponge unsorted.txt