#!/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