Loop through the folders and search for particular string in files


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
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
Code:
 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;

Code:
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;

Code:
 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

Last edited by rbatte1; 02-07-2017 at 07:48 AM.. Reason: Tidied up lists and added CODE tags for file contents
# 2  
Hello Siva,

I would suggest that your loop might be better like this:-
Code:
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:-
Code:
   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

Last edited by rbatte1; 02-07-2017 at 08:03 AM.. Reason: Grammar correction
# 3  
The following variant takes space or tab characters, and runs grep with many (file-)arguments
Code:
find "$folder" -type f -name "*[Ss][Qq][Ll]" -exec egrep -il "(CREATE|ALTER)[[:space:]]+TABLE" {} +

This User Gave Thanks to MadeInGermany For This Post:
# 4  
Thanks for the reply.

However the code below, did not work.

Code:
 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
# 5  
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.
This User Gave Thanks to RudiC For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Loop with Perl (string search)

I am using a perl script to reverse and complement sequences if a string is found. The script works as expected as standalone but I would like to use it in my bash file. However, I am not getting my expected result. My test.txt file >Sample_72... (8 Replies)
Discussion started by: Xterra
8 Replies

2. Shell Programming and Scripting

Recursive search for string in file with Loop condition

Hi, Need some help... I want to execute sequence commands, like below test1.sh test2.sh ...etc test1.sh file will generate log file, we need to search for 'complete' string on test1.sh file, once that condition success and then it should go to test2.sh file, each .sh scripts will take... (5 Replies)
Discussion started by: rkrish123
5 Replies

3. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

4. Shell Programming and Scripting

Some manipulations with files and folders. (loop, find, create and remove)

Hello! I need to realize such task. 1. In my user's home dir I have folder1; 2. In folder1 I have some (various count) subfolders with random names; 3. In these subfolders I have one file anyname.pdf (various name in each subfolder) and file content.txt (constant name in each subfolder) ##... (7 Replies)
Discussion started by: optik77
7 Replies

5. UNIX for Dummies Questions & Answers

Loop through Sub Directories and search for set of files

I have the below directory in unix environment /home/bkup/daily: ls -lrt drwxrwx--x 2 user user 256 Jan 12 18:21 20110112/ drwxrwx--x 2 user user 256 Jan 13 17:06 20110113/ drwxrwx--x 2 user user 256 Jan 14 16:44 20110114/ drwxrwx--x 2 user user ... (2 Replies)
Discussion started by: prasannarajesh
2 Replies

6. Shell Programming and Scripting

Loop folders, delete files, copy new ones

Folks, I am hopeful that you may be able to help me out with writing a script that can be run nightly (as cron?) to loop through all subfolders within the "/media" directory, delete all of the files in each of them, and then copy in all of the files from the "/home//sansa" directory to each of... (6 Replies)
Discussion started by: acraig
6 Replies

7. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

8. Shell Programming and Scripting

Search files from sub folders

A folder contains 30-50 subfolders generated on a daily basis, each subfolder contains more than 10-20 files of .html.gz files format. we have to manually convert the .html.gz file to .html format on a daily basis available in all the sub folders. the procedure we are following is copying... (3 Replies)
Discussion started by: Nareshp
3 Replies

9. Shell Programming and Scripting

How to exclude folders/files in search?

I have a directory with about 20 folders and many different types of files. I need to search for files and gzip in all the directories except for 1 directory. How do you exclude a directory? (2 Replies)
Discussion started by: bbbngowc
2 Replies

10. UNIX for Dummies Questions & Answers

string search in folders with particular multiple file extension

Hi, I am newbie in UNIX so please excuse for my questions. Is there a a way to search for string in files within folder and sub folder in particluar file extensions. Ex. search for ABC in folder 'A'(including it's sub folders) in html, xml files. Thanks, Ani (2 Replies)
Discussion started by: anikanch
2 Replies

Featured Tech Videos