4 ms·
I have written several scripts -- some throwaway, some not -- that use ed. If you know what you need to do to edit a file and need it automated, ed is just supe
by djhaskin987 4y ago
I have written several scripts -- some throwaway, some not -- that use ed. If you know what you need to do to edit a file and need it automated, ed is just super handy. Here is a shell script that deletes all comments from a yaml file, then comments out a particular key:
ed $file << COMMANDS
g/^#.\*$/d
/yaml_key:
s/^/#/
w
q
COMMANDS
- meatmanek 4y agoIn most cases I find `sed -i` to be more convenient than using ed in scripts, but the paradigm is a little different.
- jrumbut 4y agoI finally had an occasion to write a .sed script a few weeks ago and it was one of the highlights of my career.
- gnubison 4y agoExcept for when it breaks on FreeBSD or macOS. The most portable utility nowadays for desktop systems is ex, actually (because some Linux distros don’t include ed).
- marcosdumay 4y agoWell, of course, sed is not the stantard... It is still much more usable than ed. At least, it makes it easier to debug your script.
- gnubison 4y agoIn what way? Ed is interactive so you’d be able to act out your script line by line and check for mistakes, which seems easier to me. If you’re cognizant about the differences between ed and ex, you could even debug with vi.
- marcosdumay 4y agoHum... I was thinking on the lines of debugging an entire shell script. Sed has way more output options. For a single edition script (one shell command), yeah, I'd say there is no large difference.
- hulitu 4y agoLet me guess:Redhat?
- deleted 4y ago[deleted]
- krylon 4y agoThat's nice. I'd probably user Perl for this, but I happened to stumble upon Perl just in time to make sure I'd never become a shell wizard. Anything more complex than a couple of canned commands and maybe a loop, and I go for Perl. One thing I do like about ed, though, is that when I run eshell within emacs, I can call ed from there. Since ed doesn't make any assumptions on the capabilities of the terminal it runs on, it works perfectly well in that environment, and being able use another editor from within my editor made me very happy for a couple of minutes. ;-)
- emmelaich 4y ago`diff -e` and `patch -e` produce and consume `ed` commands