1846394 Members
3264 Online
110256 Solutions
New Discussion

Re: search for string

 
rogerio_1
New Member

search for string

Hi,

I need to search through all the files on my server and produce a list of only those files that contain the text FPRD within the file. I want to ignore binary files, if possible.

Thanks ..
7 REPLIES 7
RAC_1
Honored Contributor

Re: search for string

find . -type f -print \; | while read a
do
grep -q 'FPRD' ${a}
if [ "$?" -eq "0" ];then
echo "File has FPRD in it-${a}"
else
exit 1
fi

There is no substitute to HARDWORK
Ninad_1
Honored Contributor

Re: search for string

Rogerio,

Try the following

find ./ | while read filename
do
if test `file $filename | cut -f 2 -d ":" | grep -c text` != "0"
then
grep -l test $filename
fi
done

Regards,
Ninad
James R. Ferguson
Acclaimed Contributor

Re: search for string

Hi Rogerio:

This will restrict your search to "text" files, grep for your token and output the name of the file(s) containing the matching tokens:

# cat ./match
#!/usr/bin/sh
# $1=directory $2=token_to_match
find $1 -xdev -type f -exec file {} \; | \
awk -v TOKEN=$2 '/text$/ {split($1,a,":");
system("grep " TOKEN " " a[1] " /dev/null")}'
exit

...run as:

# ./match path token

Regards!

...JRF...
Frank de Vries
Respected Contributor

Re: search for string

I usually to the following

In a directory,
use (ls -R1) for recursive:

for afile in $(ls -1)
do
if (file $afile | grep ascii)
then
grep -in "your string" $afile
fi
done

Look before you leap
Antonio Cardoso_1
Trusted Contributor

Re: search for string

Hi rogerio,

find / -type f -exec grep -l FPRD {} \;

will do that.
Arturo Galbiati
Esteemed Contributor

Re: search for string

Hi,
cd
grep FPRD $(file *|grep text|cut -d":" -f1)

HTH,
Art
Arturo Galbiati
Esteemed Contributor

Re: search for string

Hi,
my previous reply look for FPRD in a directory only.

If you want to look for teh same on all your system:
# Search string "hello world" only in text files including subdirectories
grep "hello world" $(find ./ -name "*" -print -exec file {} \; | grep text | cut -d ':' -f 1)

HTH,
Art