Register
It is currently Fri Dec 19, 2014 8:41 am

Recursive grep ending after first file is matched.


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Tue Feb 21, 2012 3:18 pm   

Joined: Tue Feb 21, 2012 3:07 pm
Posts: 2
I'm trying to grep through a directory and find files that have patterns to tell basically if that account is using mysql or oracle, and various other things. So far I have this, which works, but will print multiple files if it doesn't leave the current directory which I can see by the code that it makes sense. My question is, is there a way to stop a recursive grep after it is found once in a file, and prevent it from searching through the next file, similar to grep -m 1.

Code:
for j in `ls /var/www`; do
# Set variables as false
  a=1
  b=1
  c=1
  echo ""
  echo "-------------$j--------------"

# Start loop through files
    for i in `ls /var/www/$j`; do

      if [[ $a == 1 && $b == 1 && $c == 1 ]]; then
            grep -wrl "test1" /var/www/$j/$i && a=0 && echo -e "\ttest1 found"
            grep -wrl "test2" /var/www/$j/$i && b=0 && echo -e "\ttest2 found"
            grep -wrl "test3" /var/www/$j/$i && c=0 && echo -e "\ttest3 found"
      elif [[ $a != 1 && $b == 1 && $c == 1 ]]; then
            grep -wrl "test2" /var/www/$j/$i && b=0 && echo -e "\ttest2 found"
            grep -wrl "test3" /var/www/$j/$i && c=0 && echo -e "\ttest3 found"
      elif [[ $a == 1 && $b != 1 && $c == 1 ]]; then
            grep -wrl "test1" /var/www/$j/$i && a=0 && echo -e "\ttest1 found"
            grep -wrl "test3" /var/www/$j/$i && c=0 && echo -e "\ttest3 found"
      elif [[ $a == 1 && $b == 1 && $c != 1 ]]; then
            grep -wrl "test1" /var/www/$j/$i && a=0 && echo -e "\ttest1 found"
            grep -wrl "test2" /var/www/$j/$i && b=0 && echo -e "\ttest2 found"

      elif [[ $a == 1 && $b != 1 && $c != 1 ]]; then
            grep -wrl "test1" /var/www/$j/$i && a=0 && echo -e "\ttest1 found"
      elif [[ $a != 1 && $b == 1 && $c != 1 ]]; then
            grep -wrl "test2" /var/www/$j/$i && b=0 && echo -e "\ttest2 found"
      elif [[ $a != 1 && $b != 1 && $c == 1 ]]; then
            grep -wrl "test3" /var/www/$j/$i && c=0 && echo -e "\ttest3 found"
      fi

    done

done


Top
 Profile  
 PostPosted: Tue Feb 28, 2012 1:20 pm   
User avatar

Joined: Sat Jul 02, 2011 10:43 am
Posts: 23
Location: Devon,UK
Basic is this
Code:
#!/bin/bash

grepit ()
{

   while read
      do
         if grep -iHnm  1 "$1" "$REPLY";then
            return
         fi
      done < <(ls)
}



grepit "sudo"


Searches through the curent folder for the first occurrence of "sudo" then exits after printing the filename and line number, just modify the code to suit yourself.


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP