Wednesday, January 30, 2008

Regular Expressions Across Multiple Lines

I was looking at this huge java file which had hundreds of SQL statements and thousands of java code interleaved like a spaghetti ! What I really needed was to analyze all the SQL statements in the file since they were all related to a single application which we were supposed to modify. I needed a mechanism to extract all the SQL statements from the file for sake of brevity.

Simple regular expressions using vim or sed did not work since they all deal with a single line, but each one of them provide a mechanism to match regular expression over multiple lines.

sed - the following command will extract all lines between a 'select' and the first ';' following that. Hence you would have printed all the sql queries from a file

sed -ne '/^select/,/;/p' SqlIntensiveFile.java

vim - using the 'g' command can achieve the same. For e.g. the following will delete all the sql queries from the file.

%g/select/,/;/d

No comments: