> Let's look at a typical novice's session with the mighty ed:
golem$ ed
?
help
?
?
?
quit
?
exit
?
bye
?
hello?
?
eat flaming death
?
^C
?
^C
?
^D
?So I went to the internet archive, downloaded almost all of it (all of the stuff that was there), put it through lynx, to text and from there created markdown.
So at some point I had 100-ish files and I wanted to put some yaml at the top of every markdown file. While playing around with how I wanted to do this at some point I needed to make some changes. I was faced with needing to do certain simple edits in the yaml front-matter of every one of a very large number of files.
Now you could just make a vim/emacs macro and run it on every file and that would be fine, or you could use ed in a shellscript. Something like this:
find . -type f -name "*.md" | while read f ; do
ed "${f}" <<__DONE
1,^--g/^foo/d
wq
__DONE
done
So what have we achieved? We deleted lines which start with "foo" but only from the top until the first line that starts with "--". (ie in the yaml). That range address wouldn't work in sed, or not as easily. We can also do things like insert a line just after something or whatever. These are tricks that are worth learning, and everyone who does unix shellscripting should have "ed with a heredoc" in their bag of tricks. 1,/^--/ g...
Unless forward slashes aren't required to indicate a regex based address.One thing I do to create my ed scripts is to actually read the output of ls with some file pattern and then use a vim macro to edit the output as follows:
ed <<EOF
e file1
commands
w
e file2
commands
w
...
wq
EOF
The gnu info page for ed is a very good reference to determine what commands are available and how addressing works.