aboutsummaryrefslogtreecommitdiff
path: root/section-parse.rb
blob: 0d63f38ea745e63f29efb0677cc1ec0aaf54bd9c (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
40
41
42
43
44
45
46
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