diff options
author | Paul Duncan <pabs@pablotron.org> | 2023-03-02 17:09:02 -0500 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2023-03-02 17:09:02 -0500 |
commit | 14419f066973d26192bea9b97707bd872af772c7 (patch) | |
tree | e32224d068e8df8f387127a10efa3721d4a269d8 /section-parse.rb | |
download | section-parse-examples-14419f066973d26192bea9b97707bd872af772c7.tar.bz2 section-parse-examples-14419f066973d26192bea9b97707bd872af772c7.zip |
initial commit
Diffstat (limited to 'section-parse.rb')
-rw-r--r-- | section-parse.rb | 47 |
1 files changed, 47 insertions, 0 deletions
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 |