Operating System - HP-UX
1819796 Members
3120 Online
109607 Solutions
New Discussion юеВ

Search all text files for a string

 
SOLVED
Go to solution
Darrell Allen
Honored Contributor

Search all text files for a string

Hi all,

I'm trying to search all text files on the system for a specific string. My results haven't been very good so far. I'm having a problem selecting only text files, I'm missing some files I shouldn't, and some lines are too long for vi (when I try to edit the results file).

Anyone got something to help?

Thanks,
Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
10 REPLIES 10
Jeff Machols
Esteemed Contributor

Re: Search all text files for a string

try this

find / -type f | xargs grep string
A. Clay Stephenson
Acclaimed Contributor

Re: Search all text files for a string

Hi Darrell:

You might try something close to this:

find . -type f | while read X
do
file ${X} | grep -q -i "ascii text"
STAT=$?
if [ ${STAT} -eq 0 ]
then
grep -q -i "Your target string" ${X}
STAT=$?
if [ ${STAT} -eq 0 ]
then
echo "File: ${X}"
grep -i "Your target string" ${X}
fi
fi
done

You could, of course also pass in "Your target string" as $1
If it ain't broke, I can fix that.
Wodisch
Honored Contributor
Solution

Re: Search all text files for a string

Hello Darrell,

this is not so simple, really!
Usually something like this is working, but "file" tests only for the first 128 bytes of a file...

find / -type f -print |
while read name; do case $(file $name) in
*text*) grep -i "PATTERN" $name /dev/null ;;
esac;done

First, you get only the files (no devices, pipes, directories, links), then you select the *text files* (ascii, C, awk, english...), and then you use "grep" on those. The additional parameter "/dev/null" forces "grep" to show the name of file and the line containing the match...

HTH,
Wodisc
Roger Baptiste
Honored Contributor

Re: Search all text files for a string

Hi Darell,

find $DIR | xargs file |grep "ascii text" | awk '{ print $1}' | sed 's/://g' >file_list

for i in `cat file_list``
do
grep ""yourstuff"" $i
if [ $? -eq 0 ]
then
echo "$i" >>Caught
done

HTH
raj
Take it easy.
Thom Cornwell
Frequent Advisor

Re: Search all text files for a string

find ./ -name "*" -exec grep -il 'Pattern_to_match' {} \;|xargs file|grep asci>log might work
Paula J Frazer-Campbell
Honored Contributor

Re: Search all text files for a string

Hi

find . -type f -print -exec grep {} \;

search for a string inside files.

HTH

Paula
If you can spell SysAdmin then you is one - anon
Steven Gillard_2
Honored Contributor

Re: Search all text files for a string

I recently had to do this for an Oracle upgrade - searching for all scripts etc that referred to the old ORACLE_HOME.

In the end I simply resorted to using "grep -l" to print the file name rather than the matching lines. ie:

# find / -type f | xargs grep -l "string" > /tmp/log 2>&1

That way you just get a list of files that contain the match and you can decide which ones are binary and which ones arent.

Regards,
Steve
Darrell Allen
Honored Contributor

Re: Search all text files for a string

Hi all,

Thanks for the replies. They are appreciated! Just wanted you to know I'm still testing these suggestions and haven't forgotten to assign points.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Magdi KAMAL
Respected Contributor

Re: Search all text files for a string

Hi Darrell,

Consider the script lookForString.sh with two arguments : arg1 is the directory under which you are looking for string and arg2 is the string you are looking for.

find $1 -type f | while read File
do
file ${File} | grep -q -i "ascii text"
rFile=$?
if [ ${rFile} -eq 0 ]
then
grep -q -i "Your target string" ${File}
rString=$?
if [ ${rString} -eq 0 ]
then
echo "### File '${File}' contains string '$2'"
fi
fi
done

Magdi
Darrell Allen
Honored Contributor

Re: Search all text files for a string

Hi again,

Thanks for everyone's help. Wodisch sums up my testing rather well: "this is not so simple, really!"

My problem was also due to a database upgrade (Progress 83b to 83d). Why do people hard-code something like a version number in scripts? Haven't they ever heard of symlinks? I just finished going thru the resulting list of 415 files that contained 83b and changing them so that they don't reference a version number any more! Thankfully I won't have this problem again!

I tried each of the suggestions above (with some minor, I think, changes). See the attachment for what I ran, timex output, and problems noted with each. Note that each of the 4 scripts in the attachment found the same number of files if I limited my find to /etc. When finding from "/" is where the miscellaneous problems arose.

Steven's suggestion to use "grep -l" to list the filenames without the line that matched was a big help. Filenames were what I really wanted anyway and solves the problem with some lines of my output being too long for vi, awk, etc.

I think another problem is using xargs with the large number of files returned by find. Perhaps this can be avoided by coding a loop on all mounted filesystems and using the -xdev arg of find. I didn't try that. In any event, that problem didn't seem to happen in the following (which is what worked best for me):

find / -type f -exec grep -l 83b {} \; | xargs file | grep text | awk -F: '{print $1}'

Thanks again,
Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)