aboutsummaryrefslogtreecommitdiff
path: root/section-parse-insane.py
blob: 6dc531e226f402c3683d962412956352df9bf4f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/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, 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")]))