Matching IF


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Matching IF
# 1  
Old 08-27-2010
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
# 2  
Old 08-27-2010
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.

Code:
#!/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:
Code:
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.
# 3  
Old 08-27-2010
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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to combine all matching dates and remove non-matching

Using the awk below I am able to combine all the matching dates in $1, but I can not seem to remove the non-matching from the file. Thank you :). file 20161109104500.0+0000,x,5631 20161109104500.0+0000,y,2 20161109104500.0+0000,z,2 20161109104500.0+0000,a,4117... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

3. Shell Programming and Scripting

Insert lines above matching line with content from matching

Hi, I have text file: Name: xyz Gender: M Address: "120_B_C; ksilskdj; lsudlfw" Zip: 20392 Name: KLM Gender: F Address: "65_D_F; wnmlsi;lsuod;,...." Zip:90233I want to insert 2 new lines before the 'Address: ' line deriving value from this Address line value The Address value in quotes... (1 Reply)
Discussion started by: ysrini
1 Replies

4. Shell Programming and Scripting

Pattern matching

Hi guys I am trying to create a regular expression to match a word with the following pattern: any letter(one or more occurence) followed by a digit(one or more occurence) followed by any letter(one or more occurence) I have tried the following regex but it fails to find what I need: $... (4 Replies)
Discussion started by: aoussenko
4 Replies

5. Shell Programming and Scripting

matching in loop

hello all, this is probably very simple for you guys but i am trying to achive something like below...i have 2 files...file 1 has entries like below DB1:NS DB2:NS DB3:NS DB4 DB5:NS and file2 as below DB1 DB2 DB5 i was to write a loop statment were if there is a matching... (1 Reply)
Discussion started by: abdul.irfan2
1 Replies

6. Shell Programming and Scripting

awk: matching and not matching

Hello all, simple matching and if not match problem that i can't figure out. file1 hostname: 30 10 * * * /home/toto/start PROD instance_name1 -p 00 9 * * * /home/toto/start PROD instance_name2 -p 15 8 * * * /home/toto/start PROD instance_name3 -p hostname2: 00 8 * * *... (5 Replies)
Discussion started by: maverick72
5 Replies

7. Shell Programming and Scripting

Pattern Matching

Hi Folks, I have the following requirement: I have a file that is containing numerous queries. The tables name mentioned in the queries are in the following format : SchemaName.Tablename. e.g COPDB.TableName. I need to take out all the COPDB.TableName pattern and write it to a different... (6 Replies)
Discussion started by: Siv_Pat
6 Replies

8. UNIX for Dummies Questions & Answers

Pattern Matching

Hi Folks, I have the following requirement: I have a file that is containing numerous queries. The tables name mentioned in the queries are in the following format : SchemaName.Tablename. e.g COPDB.TableName. I need to take out all the COPDB.TableName pattern and write it to a different... (0 Replies)
Discussion started by: Siv_Pat
0 Replies

9. UNIX for Dummies Questions & Answers

Matching

Can anyone give a sample example of comparing two strings using matching concept in unix shell programming. say we have two strings,S1 = ravi and S2 = kiran how can i use matching concept to compare S1 and S2 Can anyone please help me. (2 Replies)
Discussion started by: veerumahanthi41
2 Replies

10. UNIX for Dummies Questions & Answers

Matching string

Hello, i have a program where i have to get a character from the user and check it against the word i have and then replace the character in a blank at the same position it is in the word. (7 Replies)
Discussion started by: nehaquick
7 Replies
Login or Register to Ask a Question