Is there any non-artistic reason why one would like to do it this way instead of using any programming language that's actually any readable?
sed and it's bigger brother awk, are excellent tools to have in your daily use toolkit.
O'Reilly have an excellent book: "sed & awk Book by Arnold Robbins and Dale Dougherty"
For awk, there is the classic book:"The AWK Programming Language by Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger"
I believe The Unix Programming Environment used this technique to create their index.
Unix also came with dedicated to the purpose tools for making indexes. ptx is one.