Matching IF

Hi All,
I have a IF loop followed by THEN and a a closing FI.I am trying to find the matching 'FI' for 'IF'.I am wokring on modifying 2000 line code and its become extremely difficult to find the matching of 'IF' manually for on e of the block.
Can you guys throw some light please?

Thanks
Vemana

This is simplistic, but might be all that you need to help. It prints if/else/fi lines along with their line numbers. If you have embedded awk and leave a space between the if and opening parenthesis, it will trip over that and you'll need to change it a bit. Won't catch elif statements either, but it should be obvious how to change the script if you need it to.

#!/usr/bin/env ksh
awk '
        {
                if( $1 == "if"  || $1 == "fi" || $1 == "else" )
                        printf( "%04d: %s\n", NR, $0 );
        }
' <name-of-source-file

Output looks something like:

0042: if [[ -z $1 ]]
0045: fi
0050:           if (( $# > 1 ))
0053:           fi
0065:                   if [[ ${2:-nothing} == *master* ]]
0068:                   fi
0080: if [[ $url != "random" ]]
0084: fi
0088: if (( $set_vol > 0 ))
0091: fi
0093: if [[ -n $url ]]
0095:   if [[ $url == "random" ]]
0108:   else
0111:   fi
0112: else
0114: fi

Using the output from this script and an editor that gives you line numbers, you should be able to easily match block start/end.

First of all thanks for the quick response.
This looks complicated.I was thinking there would be a simple command something like % which matches the braces.

My IF - ELSE - FI block is well over 800 lines,I am still not sure how I could find the matching FI by running your code.Apologize if I did not understand your code

Thanks