5 ms·
ed is perfect in shell scripts: % cat <<- _EOF_ | ed -s filename ed commands... _EOF_ works on all UNIX-like operating systems and it means that tem
by Annatar 10y ago
ed is perfect in shell scripts:
% cat <<- _EOF_ | ed -s filename
ed commands...
_EOF_
works on all UNIX-like operating systems and it means that temporary files are no longer necessary. (GNU sed -i is an architectural violation of the idea of a stream editor, and it isn't available everywhere.)
- tincholio 10y agoI routinely automate text-mangling tasks with short ex (most of them would probably work in ed, too) scripts. I find it simpler than using sed, most of the time. If you're comfortable using ex commands in vim, then it's almost a seamless transition
- protomyth 10y agoFreeBSD found out it is not perfect since they had to patch patch because it called ed. People figured out how to run arbitrary scripts in a privileged manner.
- Annatar 10y agopatch(1) outputs ed(1) commands. It always worked that way, and it demonstrates the power of the "do one thing and do it well": patch just does the analysis and generates the appropriate editting commands, but the editting is passed on to a program specialized for just that: ed(1). It as also a good showcase for UNIX modularity principle.
- protomyth 10y agoIt's a good demonstration up until it turns into an attack vector. Then it violates the "do one thing well" part.
- 101km 10y agosed -e s/foo/bar/ | sponge filename https://joeyh.name/code/moreutils/ https://joeyh.name/code/moreutils/
- OskarS 10y agoYou can also use the -i flag of sed to edit a file in-place. It leaves a backup file with the original in case you messed up.
- Annatar 10y agoI explicitly wrote: GNU sed -i is an architectural violation of the idea of a stream editor, and it isn't available everywhere. precisely to preclude anyone from suggesting using GNU sed -i.
- Annatar 10y agosponge is found on GNU/Linux, but is not a UNIX standard; when writing shell programs one must never assume GNU/Linux, because, as my ed(1) example shows, it is completely unnecessary and makes the code unportable for no reason whatsoever. Also by relying on external tools, you have now forced your users down the dependency hell path.
- qwertyuiop924 10y agoAssume the environment you're writing scripts for. Most scripts need only be run on one machine, and thus one platform. Most that don't still run only on one platform. If you need a portable shell script, by all means write one, but that's rarely the case.
- Annatar 10y agoAssume you know how to write shell programs which work everywhere without modification; why would you make your program unportable on purpose?
- qwertyuiop924 10y agoIn some cases, it can significantly shorten and clarify the code. That's a Good Thing. Nobody wants to debug shell scripts.
- kovek 10y agoIt is unclear what this tries to achieve. Could you please explain why this removes the need for temporary files?
- deleted 10y ago[deleted]
- Annatar 10y agoIf you want to perform an in-place edit, and you want to do it in a portable manner (not using GNU sed -i), you'd have to use sed with at least one temporary file: % sed -e 's/something/other/' somefile > /tmp/temp.$$ && mv /tmp/temp.$$ somefile now, using ed (or AWK) and the heredoc with ed commands above, you can mmap(2) the entire file into memory, perform desired editting, and write it all out in one go. No more temporary files!
- falcolas 10y agoSed, when operating on a file, creates a temporary file alongside the target file.
- eriknstr 10y agoUseless use of cat though. You could achieve the same thing by: % ed -s filename <<- EOF ed commands... EOF
- deleted 10y ago[deleted]