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.rb | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 section-parse.rb (limited to 'section-parse.rb') diff --git a/section-parse.rb b/section-parse.rb new file mode 100644 index 0000000..0d63f38 --- /dev/null +++ b/section-parse.rb @@ -0,0 +1,47 @@ +#!/usr/bin/env ruby + +# +# section-parse.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.rb < input.txt +# bar.1 +# bar.2 +# bar.3 +# bar.4 +# + +# default state +in_body = false + +# read lines +ARGF.readlines.map { |s| s.strip }.each do |s| + # check if either of the following conditions are true: + # + # 1. we are outside the section and the current line is the matching + # section header + # 2. we are in the section body and the current line is empty + if (!in_body && s =~ /^test-text ".*\/foo"$/) || (in_body && s =~ /^\s*$/) + # toggle state + in_body = !in_body + elsif in_body + # print "bar.NUMBERS" line suffix + puts s.gsub(/.*(bar\.\d+)$/, '\1') + end +end -- cgit v1.2.3