title: How to parse records line by line and print only selected fields
body: <p>I have a huge file with the structure similar to:</p>
  
  <pre><code>PMID- 1
  OWN - NLM
  STAT- PubMed-not-MEDLINE
  LR  - 20191218
  TI  - Synthesis and Characterization of a Fluorescence Probe of the Phase Transition
        and Dynamic Properties of Membranes.
  PG  - 5714-5722
  LID - 10.1021/bi00294a006 [doi]
  AB  - We describe the synthesis and characterization of a new fluorescence probe whose 
        emission spectra, anisotropies, and wavelength-dependent decay times are highly
        sensitive to the phase state of phospholipid vesicles. 
  PST - ppublish
  SO  - Biochemistry.
  
  PMID- 2
  STAT- Publisher
  PB  - National Academies Press (US)
  TI  - The National Academies Collection: Reports funded by National Institutes of
        Health
  AID - NBK547296 [bookaccession]
  AID - 10.17226/19375 [doi]
  
  PMID- 3
  STAT- Publisher
  DA  - 20140815
  ISBN- 030903339X
  PB  - National Academies Press (US)
  DP  - 1983
  BTI - Community Oriented Primary Care: New Directions for Health Services Delivery
  CN  - Institute of Medicine (US) Division of Health Care Services
  </code></pre>
  
  <p>The <strong>task</strong> is to print lines with <code>PMID</code>, <code>TI</code>, and <code>AB</code> fields only, with the following constraints:</p>
  
  <ul>
  <li>Print PMID field only if TI field is present in a record</li>
  <li>Records should be separated with a single blank line</li>
  </ul>
  
  <p>In original data <code>AB</code> field is present only if <code>TI</code> field is present in the record. The result should be:</p>
  
  <pre><code>PMID- 1
  TI  - Synthesis and Characterization of a Fluorescence Probe of the Phase Transition
        and Dynamic Properties of Membranes.
  AB  - We describe the synthesis and characterization of a new fluorescence probe whose 
        emission spectra, anisotropies, and wavelength-dependent decay times are highly
        sensitive to the phase state of phospholipid vesicles.
  
  PMID- 2
  TI  - The National Academies Collection: Reports funded by National Institutes of
        Health
  </code></pre>
  
  <p>The current solution is:</p>
  
  <pre><code>import re
  
  with open("input.txt", "rt") as in_file:
      prog = re.compile("^(....)- (.*)$")
      for line in in_file:
          line = line.rstrip()
          match = prog.match(line)
          if match:
              tag = match.groups()[0]
              field = match.groups()[1]
          # If "PMID" tag, print it but only if "TI" field is present in the record
          if tag == "PMID":
              pmid = line
          # If "TI" line, print it
          if tag == "TI  ":
              print(line)
              ti_line = True
          # If line is a "continuation line" and we are in TI field
          elif line.startswith("      ") and ti_line:
              print(line)
          else:
              ti_line = False
          # If "AB" line, print it
          if tag == "AB  ":
              print(line)
              ab_line = True      
          # If line is a "continuation line" and we are in AB field
          elif line.startswith("      ") and ab_line:
              print(line)
          else:
              ab_line = False
  </code></pre>
  
  <p>with the output:</p>
  
  <pre><code>TI  - Synthesis and Characterization of a Fluorescence Probe of the Phase Transition
        and Dynamic Properties of Membranes.
  AB  - We describe the synthesis and characterization of a new fluorescence probe whose
        emission spectra, anisotropies, and wavelength-dependent decay times are highly
        sensitive to the phase state of phospholipid vesicles.
  TI  - The National Academies Collection: Reports funded by National Institutes of
        Health
  </code></pre>
  
  <p><strong>Questions:</strong></p>
  
  <ul>
  <li>What is the best way to include <code>PMID</code>s in the output?</li>
  <li>How to separe records with the blank line?</li>
  </ul>