9 comments

[ 4.5 ms ] story [ 40.6 ms ] thread
Hats off! It's like watching someone tie shoelaces with their teeth while juggling plates. Today a flip-flop, tomorrow the NSO emulating a LISP machine to crack your phone passwords in situ.
It reminds me when I was working in the defence sector in an airgapped building with no windows and a 12 month purchase order lead time on anything actually usable. We used to do stuff like this just to not be bored or frustrated.

I wrote a full bytecode runtime in VBA inside MS Word that generated dynamic documents on the fly and a high level language and compiler for it that our test systems could emit stuff to. I did this because I was bored writing up test results by hand. I dread to thing of the poor guy who inherited it when I quit.

I once saw an article about something similar, where the author had written a primitive circuit simulator but using shell pipes and /dev/null and /dev/zero as a current sink/source (or something like that). It was very cool, but I never managed to find the article again.

Edit: found it [0]! and it's from Linus Akesson, a total legend.

[0] https://www.linusakesson.net/programming/pipelogic/index.php

That thing was jaw dropping to me. I do wish his code was commented so I could follow it better.
I just recently decided to skim the GNU Sed manual and it honestly doesn't surprise me one bit that this is possible. Sed is basically a bytecode VM that only does string manipulation.
Did you acquire any useful new tricks from this experience?
Yeah, it was interesting to learn how sed addresses[0] work, particular regex and range addresses in combination.

Basically, you can select a range of lines using regexes. For example, if I wanted to get the text under one header in a Markdown document, I could write something like this:

  sed -n '/^## Overview/,/^#/ { /^#/b; p }' README.md

  - The '-n' flag tells sed not to print all matched lines by default.
  - '/^## Overview/,/^#/' is a range address that matches all lines from
    one starting with "## Overview" to another line starting with "#", inclusive.
  - '{ /^#/b; p }' is a command group that's executed for each matched line.
  - '/^#/b' uses the 'b' (branch) command to skip lines starting with "#" to
    avoid printing the header lines, which are included in the range.
  - Finally, 'p' is the print command, which tells sed to print all the other
    lines.
That said, I mainly read it because I've always used things like Bash's built-in parameter expansion[1] and regex capability[2] instead of sed, and I mostly think I confirmed that bias. I'll probably turn to sed for certain string manipulations from now on, but I'm not sure I'll be using it a whole lot more often.

[0] https://www.gnu.org/software/sed/manual/sed.html#sed-address... [1] https://wiki.bash-hackers.org/syntax/pe [2] https://www.linuxjournal.com/content/bash-regular-expression...