Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

An Easy Way To Test sed Expressions

TL;DR

Make an `input.txt`` with some content and a `sed_runner.bash`` file with this:

sed_runner.bash

#!/bin/bash

cat input.txt | sed -E 's/\!\[Image: [^]]+\]\(([^\)]+)\)/<<\1|img>>/g'

Then setup watchexec to run the bash script every time it changes so you can see the output with this:

Code

watchexec --project-origin . -w sed_runner.bash ./sed_runner.bash

The Problem

Reducing friction and time to iteration is huge for me. I work best when the mechanics get out of the way.

I had a lot of clean up to do while transitioning my site from markdown/org-mode to neopolian. No surprise given that there' twenty years of files.

The main work was all done with rust's _nom__ parser. The clean up was done with the sed command line tool. I ended up with a bunch of commands like:

Code

sed -E -i "" 's/\!\[Image: [^]]+\]\(([^\)]+)\)/<<\1|img>>/g' a.txt

That turn's this:

<<aws-20120918--1724-02.jpg|img|caption: Image: aws-20120918--1724-02.jpg>>

Into this:

<</aws-20120918--1724-02.jpg|img>>

The Automation

Figuring out those expression took some doing, but I found a way to make it relatively painless.

My approach is to setup two files. One that has the input text of the string to work. Then I make a second file that uses _cat__ to send the input to set for testing.

That file looks something like this:

sed_runner.bash

#!/bin/bash

cat input.txt | sed -E 's/\!\[Image: [^]]+\]\(([^\)]+)\)/<<\1|img>>/g'

I `chmod u+x`bash` that file to turn it into an executable script. The last, and most important, step is to put a watcher on the script to run it every time it's saved. I use _watchexec__ for that. The command I run to start it is:

Code

watchexed --project-origin . -w sed_runner.bash ./sed_runner.bash

With all that setup it's just a matter of walking throught the sec expression to get things working by watching the output on the command line after every save.

  • retest the commands