Basic usage looks like this:
printf '%s\n' '" some commands...' 'wq' | ex -s file
Or: ex -s file <<'EOF'
" some commands...
wq
EOF
By the way, these commands are the ones that you use in your vimrc or after a colon in vim — at least, the POSIX subset of that — so any ex commands you learn translate naturally to your normal editor.That said, the "lottery factor" is often a bigger contributor to the things that land in codebases than "optimality". Plus, I've actually seen somewhere that perl is the most common binary across every system, and it's likely a larger population who know perl than ed would be my guess
What is the distribution of these different versions of Perl across various OSes and OS versions?
Hint: Lots of backward-incompatible changes tend to get made around different major versions. Having Perl 4 is not like having Perl 5 which is not like Perl 6.
Are there any contemporary distros that have the "perl" exectuable that is not Perl 5?
Perl 5 was released in 1994. It is incredibly portable. If you have a system that legitimately has Perl 4, I would love to hear about it, but …
$ docker run --rm ubuntu:22.04 bash -c 'command -v ex; command -v ed; command -v vi; command -v perl;'
/usr/bin/perl
and the same result for "debian:stable"---
edit: I just realized that's because apt is _written in_ perl, but tomato, tomahto, and it may very well be that they picked perl for that same universal-binary reason
It's GNU sed vs (Free)BSD sed, which are different enhancements of the POSIX standards for sed that went in different design directions. One could Homebrew/macports install gnu-sed on macOS to get a GNU version to write Linux-portable scripts as-needed.
I never understood the point of the -i option other than to conserve keystrokes. A temporary file is still created then removed; the -i option only saves the user from having to specify it. Maybe the intent is it is only for "one-off" use, not for use in scripts.
This will work for GNU, BSD and Plan9:
sed -n 's/old/new/wfile.tmp' file
mv file.tmp file
Or just use redirection.Given the choice between avoiding some keypresses and more portable scripts, I will keep choosing the later.
NetBSD sed may have the -i option now but I do not see anyone using it in scripts meant to be portable, like build.sh^1
1. https://ftp.netbsd.org/pub/NetBSD/NetBSD-release-9/src/build...
Also, be aware you currently have duplicated comments: https://news.ycombinator.com/item?id=31254181
This saves only lines which underwent substitution, which was probably not what you wanted.
sed -n 's/old/new/;w file.tmp' file
Unlike BSD and GNU sed, it appears that Plan9 sed will append to instead of overwite file.tmpIt also requires a space after the w command.
Here’s a device-independent variant:
title(){ tput tsl || tput -T xterm+sl tsl; printf %s "$*"; tput fsl || tput -T xterm+sl fsl; }
Note: if the terminal does not advertise support of the necessary capabilities, it falls back to using the XTerm escape sequences. + printf %s 'hello world'^M^Jhello world+ tput fsl^M^J+tput -T xterm
and that's where it cuts off but I presume ends with "^M^J" at the end :-DSo, I'll stick to my printf thanks
If you want to see what bytes it would output, use “od”:
tput -T xterm+sl tsl | od -t cAs a concrete example, my printf version works even when run inside docker, but
$ docker run --rm ubuntu:22.04 bash -c '{ tput tsl || tput -T xterm+sl tsl; } | od -c'
tput: No value for $TERM and no -T specified
tput: unknown terminal "xterm+sl"
0000000I would assume that if you run an interactive shell inside docker, TERM would actually be set correctly. It’s the same when you ssh somewhere else – the TERM environment variable is sent along, so that the remote program can see it and output the correct codes for your local terminal. Also, the docker image needs the terminfo database installed for “tput” to work.
My contribution (and yeah, I know, PR it ...) is that I get a lot of mileage out of setting the terminal title from my scripts:
and there's one for tmux, too with the two infinitely handy resources:* https://www.xfree86.org/current/ctlseqs.html
* https://iterm2.com/documentation-escape-codes.html