Loop through the folders and search for particular string in files

Hello,

Opearting System Environment : HP Unix B.11.31 U

I look for script to

  1. On specific folders list
  2. On specific filelist
  3. Search for given string

For Example :

  • r48_buildlib.txt contains
 wpr480.0_20161027
 wpr480.0_20161114
 wpr481.0_20161208
 wpr482.0_20161222
 wpr483.0_20170105
  • On the above folder, I need to search for only the *.sql or *.SQL files.
  • I tried with code below;
for folder in `cat r48_buildlib.txt`; do find "/app/oracle/build_lib/"${folder}  -exec egrep -il "create|alter|drop" || sort >> R48_filelist.log ; done;

Appreciate any help on this.

---------- Post updated at 03:13 PM ---------- Previous update was at 01:43 PM ----------

I come with solution like below;

 for folder in `cat r48_buildlib.txt` ; do   find  "/app/oracle/build_lib/"${folder}   -name  "*.[Ss][Qq][Ll]"  -exec egrep -il "CREATE TABLE|ALTER TABLE" {}  \;; done;

Now, the need is, how to exclude spaces while searching for the particular string like

  1. CREATE TABLE
  2. CREATE TABLE
  3. CREATE TABLE

Appreciate your help on this.

Thanks

Hello Siva,

I would suggest that your loop might be better like this:-

while read folder
do
   folder="/app/oracle/build_lib/${folder}"
   find ........
done < r48_buildlib.txt

Your find/grep could then become something more like this:-

   find $folder -name "*[Ss][Qq][Ll]"  -exec egrep -il "CREATE[ ]+TABLE|ALTER[ ]+TABLE" {}  \;

The expression in the egrep is looking for (in the first part) the string that starts with CREATE followed by one or more spaces then followed immediately by the string TABLE (case ignored by the -i flag as you already have) or a line matching ALTER TABLE in the same way.

I hope that this helps,
Robin

The following variant takes space or tab characters, and runs grep with many (file-)arguments

find "$folder" -type f -name "*[Ss][Qq][Ll]" -exec egrep -il "(CREATE|ALTER)[[:space:]]+TABLE" {} +
1 Like

Thanks for the reply.

However the code below, did not work.

 find  "/app/oracle/build_lib/"${folder}   -name  "*.[Ss][Qq][Ll]"  -exec egrep -il "(CREATE|ALTER|DROP)[  ]+ TABLE" {}  \;

Please note the environment is HP-Unix B.11.31

WHAT did not work? How do you expect an analysis beyond wild guesses without knowing that?

It works perfectly to print files that have the pattern in which "TABLE! is separated with a (space or <TAB>) PLUS another space. This is not what MadeInGermany proposed, btw.

1 Like