title: Script to calculate number of lines between two strings across multiple files
body: <p>I have some files which are thousands of lines in size. My task is to calculate all the lines between two strings - "access-list $f-datacenter" AND "ip access-list" in each of those files. $f is a variable which I will be putting in. The patterns looks like this  -</p>
  
  <pre><code>Beginning of file
  .
  .
  .
  .
  access-list $f-datacenter
  .
  .
  .
  ip access-list
  .
  .
  END OF FILE
  </code></pre>
  
  <p>I am working on a script which will calculate the number of lines between these two strings in multiple files. My script inputs a variable in string # and the file name from an array I define -</p>
  
  <pre><code>fnames=( x y ); for f in ${fnames[@]}; do awk '/access-list $f-datacenter/ {b=NR; next} /ip access-list/ {print NR-b-1; exit}' $f-datacenter.txt ; done
  </code></pre>
  
  <p>However, I see this script returning wrong line numbers each time it is run.  For example, if the actual line numbers are in the 600 range, the script will only show me something around 200.</p>
  
  <p>When I run the same commands individually like this I get the correct line #s -</p>
  
  <pre><code>awk '/access-list x-datacenter/ {b=NR; next} /ip access-list/ {print NR-b-1; exit}' $x-datacenter.txt
  awk '/access-list y-datacenter/ {b=NR; next} /ip access-list/ {print NR-b-1; exit}' $y-datacenter.txt
  </code></pre>
  
  <p>So, question is why does this fail in a script and how to fix it?</p>