The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.

Thursday, June 4, 2009

Traversing directory structure in perl

Today I started learning a bit of perl and created a small program to search for a particular string in all the xml files in the current directory as well as any sub-directory and printing the count of occurence of string.

Here is the code:
#!/usr/bin/perl --  
use strict;
use warnings;
use diagnostics;
use Fatal qw/:void open close/;
#first find the .xml files in the directory recursively and store in a temp list
  $f=`find . -name "*.xml" > /tmp/fileList`; 
 open TEMPFILE, "/tmp/fileList" ;
 $subflowCount = 0; 
 # traverse throygh each xml file 
 while( ){ 
 $file = $_;  
 #if yes then look for subflow tag           
    $result=`grep -i  "" $file`;   if ( $result=~m//){
print $file."\n";
 $subflowCount = $subflowCount + 1; 
 print "Total subflows are:";
 print $subflowCount; 

I have commented the code to understand what it is doing.
To run go to linux box:
do: chmod 777 
do ./
Post a Comment