title: extract text in file between nth occurence of special character
body: <p>I need to extract text in a file between two nth occurrences of a special character. Linux "fortune" data is delimited by the "<strong>%</strong>" character. I have a script that counts the number of <strong>%</strong> occurrences then picks a rnd number in range. Now I need to extract the text <strong>in a file</strong> between n-1 and n occurrences of <strong>%</strong>.</p>
  
  <pre><code>#!/bin/bash
  # get a fortune message 
  MESSAGES=$(tr -cd % &lt; fortune.dat | wc -c) # number of messages
  MSG=$(shuf -i 1-$MESSAGES -n 1)            # rnd message in range
  echo $MSG                                  # got what I expected
  awk 'NR==n' RS=% n=$MSG fortune.dat        # &lt;-- solution
  </code></pre>
  
  <p>sample <strong>fortune.dat</strong> file:</p>
  
  <pre><code>"Three people can keep a secret,
  if two of them are dead!"
  Ben Franklin
  %
  Anger is like acid, they both
  destroy the container that holds them.
  Chinese proverb
  %
  Storms make oaks take roots.
  Proverb
  %
  If you do not hope, you will not find what is beyond your hopes.
  St. Clement of Alexandra
  %
  </code></pre>