3 ms·
Wow. I've only ever used sed for simple regex operations, reading this has opened up a whole new world for me. ... I will force myself not to make use of it be
by raziel2p 5y ago
Wow. I've only ever used sed for simple regex operations, reading this has opened up a whole new world for me.
... I will force myself not to make use of it because you're a bad influence and I already have to stop myself from using too much grep/sed/awk magic, but this is seriously impressive, and an entertaining read. Well done!
- jolmg 5y agoThank you for the kind words! :)
- mananaysiempre 5y agoI can’t bring myself to write blog posts, but here’s a single-precedence-level infix-to-postfix notation transformer in sed written as part of a solution for last year’s Advent of Code 18a (https://adventofcode.com/2020/day/18 https://adventofcode.com/2020/day/18): #!/bin/sh -eu set -o pipefail sed -E \ -e 's/([+*])\s*([0-9]+)/ \2 \1 /g' \ -e ':parens' \ -e 's/([+*])\s*(\([^()]+\))/ \2 \1 /g' \ -e 's/\(([^()]+)\)/ \1 /g' \ -e '/\(/bparens' \ -e 's/\s+/ /g' -e 's/^ //' -e 's/ $//' \ -e '$a\' -e '[+ z1<r]sr z1<r p' | \ dc (I’m very, very sorry for fuelling your addiction.) UPD: The original version claimed this was a precedence parser. It isn’t, it just does parentheses :(
- jolmg 5y agoMaybe I'm doing something wrong, but passing it "3 * 4 + 5 * 6" gives me 102 instead of 42.
- mananaysiempre 5y agoYou are correct; I seemed to remember there was a catch but was too lazy to check, sorry. This isn’t a precedence parser, that was 18b, while this one solves 18a, which was just to resolve parentheses correctly and disregard precedence. (Original comment fixed.)
- nvartolomei 5y agoHow about a chess engine written in sed? https://github.com/moldabekov/chess-sed/blob/master/chess.sed https://github.com/moldabekov/chess-sed/blob/master/chess.se.... And accompanying blog post in Russian https://habr.com/ru/post/191006/ https://habr.com/ru/post/191006/.
- jolmg 5y agoThat's truly something! I feel like I can a lot from that!
- B1FF_PSUVM 5y agoI'll just leave this here: https://aurelio.net/projects/sedsokoban/ https://aurelio.net/projects/sedsokoban/