mathstodon.xyz is one of the many independent Mastodon servers you can use to participate in the fediverse.
A Mastodon instance for maths people. We have LaTeX rendering in the web interface!

Server stats:

2.8K
active users

writing about the terminal is so funny because it's like "redirects are so useful! hooray!"

"okay and also `cmd file.txt > file.txt` will just permanently delete the contents of `file.txt`”

lots of cool useful tools with the occasional horrifying fact that you just need to keep seared into your memory

(please do not explain to me why this happens, but if you personally use noclobber I would be interested to hear about how well it works for you)

@b0rk Have you seen `csh`'s "noclobber" feature? If you had `noclobber` set, then `cmd > file` would fail without erasing `file`, if it already existed. You would have to explicitly demand its clobbering with `cmd >! file`.

@mjd i've heard of it (maybe bash has it too?) but it's not clear to me if people actually use it in practice

Mark Dominus

@b0rk As to that I have no clue.

Hey, do you know about the `-o` option of `sort`? I guess it's about 90% likely that you do but if not I think you will find it tasty. I use it frequently.

@mjd I didn't! is the idea that you can do `sort blah.txt -o blah.txt`?

@b0rk Yes, exactly so. Very useful. And Perl has a `-i` ("in-place") flag which is maybe the pinnacle of Perl's do-what-I-wanted-ism. The good part of the pinnacle, I mean. It says "Iterate over the input files, as usual, except take everything you *would* have written to standard output and write it to a temp file, and each time you finish processing one of the input files, atomically rename the temp file over that input file".

So if you have 57 files and need to replace `---` with `—` in all of them you can

perl -i -lpe 's/---/—/g' file*.txt

and it does them all in-place. Or similarly:

perl -i -e 'print sort <>' file

will sort in-place.

@mjd every so often i wish i hadn't forgotten everything I once knew about perl

@shtrom @b0rk @mjd BSD `sed` also has `-i`, but with subtly different behavior when parsing arguments so it's always a pain to use it in a script that should work on Mac and Linux.

@b0rk Happily, that forgetting will only gain in value as Perl continues to fade into the liminal shadow

@mjd @b0rk or … if you use -I.bak or whatever, rename the original file to file.bak and the temp file to replace the original.