views:

58

answers:

1

I need to write a Shell Script to process a huge folder of nearly 20 levels.I have to process each and every file and check which files contain lines like select insert update

When I mean line it should take the line till I find a semicolon in that file. I should get a result like this

C:/test.java   select * from dual
C:/test.java   select * from test
C:/test1.java  select * from tester
C:/test1.java  select * from dual

and so on.Right now I have a script to read all the files

#!bin/ksh

FILE=<FILEPATH to be traversed>
TEMPFILE=<Location of Temp file>
cd $FILE    
for f in `find . ! -type d`; 
do
cat $FILE/addedText.txt>>$TEMPFILE/newFile.txt
cat $f>>$TEMPFILE/newFile.txt
rm $f
cat $TEMPFILE/newFile.txt>>$f
rm $TEMPFILE/newFile.txt
done

I have very little knowledge of awk and sed to proceed further in reading each file and achieve what I want to.Can anyone help me in this

A: 

if you have GNU find/gawk

find /path -type f -name "*.java" | while read -r FILE
do
  awk -vfile="$FILE" 'BEGIN{RS=";"}
  /select|update|insert/{
     b=gensub(/(.*)(select|update|insert)(.*)/,"\\2\\3","g",$0)
     gsub(/\n+/,"",b)
     print file,b
  }
  ' "$FILE"

done

if you are on Solaris, use nawk

find /path -type f -name "test*file" | while read -r FILE
do
  nawk -v file="$FILE" 'BEGIN{RS=";"}
  /select/{ gsub(/.*select/,"select");gsub(/\n+/,"");print file,$0; }
  /update/{ gsub(/.*update/,"update");gsub(/\n+/,"");print file,$0; }
  /insert/{ gsub(/.*insert/,"insert");gsub(/\n+/,"");print file,$0; }
  ' "$FILE"
done

Note this is simplistic case. your SQL statement might be more complicated.

ghostdog74
bad luck did not work.It said find: bad option -inamefind: path-list predicate-list
Harish
then use `-name`.
ghostdog74
Hei thanks it worked.How to take this to a text file?
Harish
you just use `>>`
ghostdog74