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 -
Beginning of file
.
.
.
.
access-list $f-datacenter
.
.
.
ip access-list
.
.
END OF FILE
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 -
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
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.
When I run the same commands individually like this I get the correct line #s -
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
So, question is why does this fail in a script and how to fix it?