The Code Diarist

A diary about code

Old Code Wins Again

September 5, 2025

Old tools can still work well, if you know how to use them. Recently I used sed, a built-in UNIX/Linux command-line tool, to reformat the html code for a web page.

The code emerged from a program that converts markdown into html. My example here came out very compactly arranged but somewhat difficult to read. Here is an example:


<h2 id="markdown-makes-a-mess">Markdown Makes a Mess
</h2><p>Markdown converts into html all right.
</p><p>However, the output appears less than ideal for human readability.
</p><p>I want to lay it out more legibly.
</p><h2 id="my-cleanup-plan">My Cleanup Plan
</h2><p>Perhaps <em>sed</em> can help.
</p>

I prefer to see it more spread out and indented.

The web swims with vendors offering to reformat html toward a more human-readable layout. A few of them even provide programs to download, compile and install for the purpose.

It was all good, modern stuff, no doubt. Yet, I wondered what could be done with older tools that were already on hand, such as sed.

Code for sed consists of single-letter commands and weird-looking strings of text called regular expressions. The following code rearranged the html satisfactorily for me.


Script #1:
/^<[^\/]/p
s/\(^<\/[[:alnum:]]*>\)\(.*$\)/\1\n\2/p

Script #2:
N
s/\(^.*\)\n\(.*$\)/\1\2/
s/^<h2.*$/  &/
s/^<p.*$/    &/
p

Here is the result:


  <h2 id="markdown-makes-a-mess">Markdown Makes a Mess</h2>
    <p>Markdown converts into html all right.</p>
    <p>However, the output appears less than ideal for human readability.</p>
    <p>I want to lay it out more legibly.</p>
  <h2 id="my-cleanup-plan">My Cleanup Plan</h2>
    <p>Perhaps <em>sed</em> can help.</p>

Exploring this old tool provided a pleasant afternoon’s entertainment. Also, it got the job done.

I saved those instructions as sed scripts for future use. I no longer need to rely upon an online service vendor or to download software someone wrote somewhere once upon a time.

It warms the heart of an old man to see an old tool win again.