From 14419f066973d26192bea9b97707bd872af772c7 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Thu, 2 Mar 2023 17:09:02 -0500 Subject: initial commit --- section-parse-insane.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 section-parse-insane.py (limited to 'section-parse-insane.py') diff --git a/section-parse-insane.py b/section-parse-insane.py new file mode 100644 index 0000000..e7c6d9e --- /dev/null +++ b/section-parse-insane.py @@ -0,0 +1,40 @@ +#!/usr/bin/python3 + +# +# section-parse-insane.py: scan standard input and do the following: +# +# 1. look for a section that begins with "test-text "STUFF/foo" and ends +# with a blank line. +# 2. print the "bar.DIGITS" suffix for each line in the section +# +# Example: +# > cat input.txt +# test-text "blah blah blah garbage/foo" +# random crap bar.1 +# random crap bar.2 +# random crap bar.3 +# random crap bar.4 +# +# test-text "blah blah blah garbage/apple" +# random crap bar.1 +# random crap bar.2 +# random crap bar.3 +# random crap bar.4 +# > ./section-parse-insane.py < input.txt +# bar.1 +# bar.2 +# bar.3 +# bar.4 +# + +# load libraries +import re +import sys + +# section and row matches +S = re.compile('(?m)^test-text "[^"]+/foo"\n((?:^[^\n]*bar\.\d+\n)+)^\s*\n') +B = re.compile('^.*(bar\.\d+)$') + +# read stdin, extract section body, split by line, map rows to suffix, +# and print to stdout +print("\n".join([B.sub('\\1', s) for s in S.search(sys.stdin.read())[1].strip().split("\n")])) -- cgit v1.2.3