Created
May 25, 2026 20:43
-
-
Save copyleftdev/7abcb4aec898161d0e84aa0dbbfb2e0e to your computer and use it in GitHub Desktop.
sponge — buffer stdin before writing: safe in-place pipeline transforms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # sponge — soak up all stdin, then write (from moreutils) | |
| # sudo apt install moreutils | |
| # THE PROBLEM: this destroys the file before sort reads it | |
| sort file.txt > file.txt # ⚠ race condition — file truncated on open | |
| # THE FIX: | |
| sort file.txt | sponge file.txt # reads all input, then writes atomically | |
| # In-place JSON formatting: | |
| python3 -m json.tool config.json | sponge config.json | |
| # In-place deduplication: | |
| sort -u output.jsonl | sponge output.jsonl | |
| # In-place cleanup: | |
| grep -v 'DEBUG' app.log | sponge app.log | |
| # Replaces the verbose temp-file pattern: | |
| # cmd file > /tmp/out && mv /tmp/out file → cmd file | sponge file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment