Sunday, November 13, 2011

A new bash implementation of ed

You know ed, right? Sure you do, because ed is the standard Unix editor! Last night I was working on a computer that didn't have ed installed, so I quickly wrote a bash implementation. It's not that hard after all! Check it out:
gniourf@somewhere:~$ while ((1)); do read; echo '?'; done
All right, apart from saying stupid things in this note, I'd just like to say that ed can be really cool to format files from a bash script. Most people use sed quite fluently, and are always tempted to solve some problems with it in scripts. ed can be useful because it's the standard Unix editor a genuine text editor after all! In a script you'll use a here document. For example, if you want to change the 40th line of a file named file to hello, gniourf_gniourf you'll probably do:
ed -s file <<EOF
40c
Hello gniourf_gniourf
.
wq
EOF
The -s option so that the sucker remains silent.
If you liked ed, you'll probably also like ex!

Cheers!

No comments:

Post a Comment