Grep multiline pattern between two words. find particular block from set of files.

Grep Multi-line pattern between two words. find particular block from set of files.
The below shell script searches the list of java files in the current directory and try to look for the catch blocks that doesn't contain any logger initiated messages.
Ex file :
public class Download extends HttpServlet {

static final String sccsId = "%Z%%M% %I% %W% %G% %U%";

private static final PDLogger msgLogger = Logger.getCDSLoggerByName(Logger.PORTAL_LOGGER);
private static final String className = Download.class.getName();

static PublicKey anonymousPublicKey;
// standard anonymous public Key
static {
try {
KeyPairGenerator keyGen = null;
keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
KeyPair keyPair = keyGen.generateKeyPair();
anonymousPublicKey = keyPair.getPublic();
} catch (Exception ex) {
msgLogger.exception(Level.DEBUG_MIN, className, "{static}", ex);
}

}
}

In the above file there is logger initiated message so, the script won't report any thing. But in case it won't find any keyword "logger" in catch block it reports the information.

Shell Script to recognize multi-line pattern :
#!/bin/sh
echo "Starting Recursive catch block search on the Dir : `pwd`"
fileCount=1
for i in `find . -name '*.java'`
do
echo "Working with file no:$fileCount and the file is:$i"
let fileCount="fileCount+1"
ttl=`cat $i | wc -l`
n=1
TTL=`cat $i | grep -c catch`
while read line
do

found=`echo $line | grep -i "catch" | sed 's/ //g' | cut -d "(" -f1`
if [ $found ]; then
while [ $n -le $TTL ]
do
begLineno=`grep -n catch $i | sed -n "$n"p | cut -d ":" -f1`
EndLineno=`sed = $i | sed 'N;s/\n/:/' | sed -n $begLineno,${ttl}p | grep -v "catch" | grep "}" | sed -n 1p | cut -d ":" -f1`
Logfound=`sed -n $begLineno,${EndLineno}p $i | grep -i Logger | sed 's/ //g' | cut -d "(" -f1 | sed -n 1p`
if [ $Logfound ]; then
echo "Logger message found In the $begLineno,$EndLineno Range Catch block" >> FoundLog.log
else
echo "No Logger messages(s)found In the $begLineno,$EndLineno Range Catch block in file $i" >> NotFound.log
fi
n=`expr $n + 1`
done
fi
done<$i
done

Hope this helps !


0 comments to "Grep multiline pattern between two words. find particular block from set of files."

Post a comment

Whoever writes Inappropriate/Vulgar comments to context, generally want to be anonymous …So I hope U r not the one like that?
For lazy logs, u can at least use Name/URL option which doesn’t even require any sign-in, The good thing is that it can accept your lovely nick name also and the URL is not mandatory too.
Thanks for your patience
~Krishna(I love "Transparency")

Popular Posts

Enter your email address:

Buffs ...

Tags


Powered by WidgetsForFree