home ~ projects ~ socials

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:

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:

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 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:

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.

TODO

    retest the commands

-- end of line --