My favorite new unix too is definitely sponge
. It’s from the moreutils suite, but the easiest way to get it on your Mac is a quick brew install sponge
.
You know how sed
can edit a file in place? Dope, right? sponge
makes it easy to do that with all the regular unix tools. If I want to fix a DOS text file by removing the carriage return, I always try doing
cat my_file.txt | tr -d "\r" > my_file.txt
But that doesn’t work because it starts writing to the file before it’s all read and a lot of times, the file ends up empty. sponge
to the rescue!
cat my_file.txt | tr -d "\r" | sponge my_file.txt
sponge
will read everything from standard in and then write it out to the given file, so this works like you want it to, even for big files.
Also great with jq
for prettifying JSON files in place too.