Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Find contents of files
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Unsupported Software
View previous topic :: View next topic  
Author Message
Nimue
Tux's lil' helper
Tux's lil' helper


Joined: 17 Sep 2006
Posts: 88

PostPosted: Thu Apr 29, 2010 3:20 pm    Post subject: Find contents of files Reply with quote

I figured I'd give this to people. What it does is it matches patterns, searches subdirectories, and returns all the files that match them. I'd been writing a loop to do this for far too long and finally just made a standalone script that takes parameters. Might be useful to someone.

Code:

#!/bin/bash
#Written by Nimue. Steal and be damned.
IFS=$'\n'

if [ $1 == "--help" ] || [ $1 == "-h" ]; then
   echo "findcontents.sh <path> <filename_pattern> <contents_pattern> <options>"
   echo "Options: -s/--show: Show string matches."
   echo "         -h/--help: Show this message."
   echo "findcontents.sh finds all files containing a string in a directory and all subdirectories"
   echo "example: findcontents.sh ./ \"*asp*\" \"<form\" --show"
   echo "example: findcontents.sh ./ \"*asp*\" \"<form\""
   exit 1
fi

if [ -n "${3}" ]; then

   if [ -n "${4}" ]; then
      if [ "$4" == "--show" ] || [ "$4" == "-s" ]; then
         for i in `find $1 -name "$2" -type f`; do cat "$i" | grep -i "$3" > /dev/null; if [ "$?" == 0 ]; then echo $i":"; echo ""; cat "$i" | grep -i "$3"; echo ""; fi; done
      else
         "There was a syntax error"
      fi
   else
         for i in `find $1 -name "$2" -type f`; do cat "$i" | grep -i "$3" > /dev/null; if [ "$?" == 0 ]; then echo $i; fi; done
   fi
else
   echo "Please include the first three fields at least."
fi


Example output:
Code:


localhost csli # findcontents.sh /usr/nimue/docs/ "*txt*" "cheese"
/usr/nimue/docs/a4/bash_assignment_1.txt
/usr/nimue/docs/a4/quiz_3/quiz3.txt
/usr/nimue/docs/bash_assignment_1.txt
/usr/nimue/docs/homework4.txt


Code:


localhost csli # findcontents.sh /usr/nimue/docs/ "*txt*" "cheese" --show
/usr/nimue/docs/a4/bash_assignment_1.txt:

file, it would sit there until, for example, someone echo'ed the word "cheese"

/usr/nimue/docs/a4/quiz_3/quiz3.txt:

CHEESE=214
if test $BLAH == $CHEESE; then

/usr/nimue/docs/bash_assignment_1.txt:

file, it would sit there until, for example, someone echo'ed the word "cheese"

/usr/nimue/docs/homework4.txt:

SWAP [pie] [cheese]
Back to top
View user's profile Send private message
avx
Advocate
Advocate


Joined: 21 Jun 2004
Posts: 2152

PostPosted: Thu Apr 29, 2010 4:51 pm    Post subject: Reply with quote

Quote:
do cat "$i" | grep -i "$3" > /dev/null; if [ "$?" == 0 ]; then echo $i":"; echo ""; cat "$i" | grep -i "$3"; echo ""; fi; done
Mighty overkill?

Code:
do grep -i ${3} ${i} > /dev/null && echo "${i}:\n`grep -i ${3} ${i}`\n" ; done
should work, too.
or
Code:
do HITS=`grep -i ${3} ${i}` && echo "${i}:\n${HITS}\n" ; done


Besides this, you could replace every "findcontents.sh" with "$0"
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Unsupported Software All times are GMT
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum