Skip to content

Instantly share code, notes, and snippets.

@copyleftdev
Created May 25, 2026 20:43
Show Gist options
  • Select an option

  • Save copyleftdev/7abcb4aec898161d0e84aa0dbbfb2e0e to your computer and use it in GitHub Desktop.

Select an option

Save copyleftdev/7abcb4aec898161d0e84aa0dbbfb2e0e to your computer and use it in GitHub Desktop.
sponge — buffer stdin before writing: safe in-place pipeline transforms
# 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