Register
It is currently Thu Jul 24, 2014 12:26 am

Recursively search .php & .inc files for a string


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Thu Sep 07, 2006 4:04 pm   

Joined: Thu Sep 07, 2006 3:56 pm
Posts: 8
Location: Toronto, Canada
Hi All,

I've spent about an hour and half banging my head against my keyboard trying to figure this one out. Any help would be great!

I'd like a script that recurses through the current directory, and identifies all files with .php & .inc extensions. The script should search through all of the identified files for a specified search string. Ideally the script would output the name of any files that contain the string, and possibly the line number of each match. Woah!

Thanks,

Neal


Top
 Profile WWW  
 PostPosted: Thu Sep 07, 2006 4:48 pm   
User avatar

Joined: Sat Sep 02, 2006 10:19 am
Posts: 13
Location: United Kingdom
stick this in a script and call it with directory to search as arg 1 and string to search for as arg2

Code:
#!/bin/bash

dir=$1
string=$2

for file in $( find $dir -name "*.php" -o -name "*.inc" )
do
   grep -l "$string" $file
   if [ $? -eq 0 ]
   then
      grep -n "$string" $file
      echo ""
   fi
done


Cheers


Top
 Profile WWW ICQ  
 PostPosted: Thu Sep 07, 2006 5:01 pm   
User avatar

Joined: Sat Sep 02, 2006 10:19 am
Posts: 13
Location: United Kingdom
Or you could just use find by itself....

Code:
find <DIR> \( -name "*.php" -o -name "*.inc" \) -exec grep -l "<STRING>" {} \; -a -exec grep -n "<STRING>" {} \;


Obviously replace DIR and STRING with appropriate values.


Top
 Profile WWW ICQ  
 PostPosted: Fri Sep 08, 2006 8:11 am   

Joined: Thu Sep 07, 2006 3:56 pm
Posts: 8
Location: Toronto, Canada
Wow! Thank you. It works perfectly.


Top
 Profile WWW  
 PostPosted: Fri Sep 08, 2006 10:01 am   

Joined: Thu Sep 07, 2006 3:56 pm
Posts: 8
Location: Toronto, Canada
I'm tyring to colour the filename output using 'echo':
Code:
#!/bin/bash

dir=$1
string=$2

for file in $( find $dir -name "*.php" -o -name "*.inc" -o -name "*.module" )
do
   echo -en "\033[01;31m"
   grep -l "$string" $file
   echo -en "\033[00m"
   if [ $? -eq 0 ]
   then
      grep -n "$string" $file
      echo ""
   fi
done


Unfortunately, the echo statement generates newline characters, even though I thought the '-n' would suppress them. Is there a way to colour the output without using echo and the colour escape sequence?


Top
 Profile WWW  
 PostPosted: Sat Sep 09, 2006 1:15 pm   
User avatar

Joined: Sat Sep 02, 2006 10:19 am
Posts: 13
Location: United Kingdom
php saved me wrote:
I'm tyring to colour the filename output using 'echo':

--snip--

Unfortunately, the echo statement generates newline characters, even though I thought the '-n' would suppress them. Is there a way to colour the output without using echo and the colour escape sequence?


No, echo does what it should. I think you've overlooked the line 'echo ""' which I put in the original. I space seperated the files to make reading easier. Doing it with colours is prettier and I think the new line you're seeing is mine :)

To change the colour you have to send the escape sequence to the terminal "echo -en" is probably the best method.


Top
 Profile WWW ICQ  
 PostPosted: Sat Sep 09, 2006 6:31 pm   

Joined: Thu Sep 07, 2006 3:56 pm
Posts: 8
Location: Toronto, Canada
I found the error that was generating the line breaks. I changed:
Code:
echo -en "\033[00m"

To:
Code:
echo -en "\033[00;m"

The absence of the semicolon was generating line breaks!


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 8 guests


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:  


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