aboutsummaryrefslogtreecommitdiff
path: root/section-parse-insane.rb
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2023-03-02 17:09:02 -0500
committerPaul Duncan <pabs@pablotron.org>2023-03-02 17:09:02 -0500
commit14419f066973d26192bea9b97707bd872af772c7 (patch)
treee32224d068e8df8f387127a10efa3721d4a269d8 /section-parse-insane.rb
downloadsection-parse-examples-14419f066973d26192bea9b97707bd872af772c7.tar.bz2
section-parse-examples-14419f066973d26192bea9b97707bd872af772c7.zip
initial commit
Diffstat (limited to 'section-parse-insane.rb')
-rw-r--r--section-parse-insane.rb35
1 files changed, 35 insertions, 0 deletions
diff --git a/section-parse-insane.rb b/section-parse-insane.rb
new file mode 100644
index 0000000..47ee824
--- /dev/null
+++ b/section-parse-insane.rb
@@ -0,0 +1,35 @@
+#!/usr/bin/env ruby
+
+#
+# section-parse-insane.rb: 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.rb < input.txt
+# bar.1
+# bar.2
+# bar.3
+# bar.4
+#
+
+# section and row matches
+S = %r{^test-text "[^"]+/foo"\n((?:^[^\n]*bar\.\d+\n)+)^\s*\n}m
+B = /^.*(bar\.\d+)$/
+
+# extract section body, split by line, map to suffix, and print result
+puts(ARGF.read.scan(S)[0][0].split(/\n/).map { |s| s.gsub(B, '\1') })