Operating System - HP-UX
1844235 Members
3389 Online
110230 Solutions
New Discussion

Re: Find Command to search only text files

 
blal
Frequent Advisor

Find Command to search only text files

Hello ,

Need a help in getting a command or a script

To find all Text files /shell scripts in a file system containing a string "word".

Your help appreciated .

Thanks,
BL .
Live and let live.
11 REPLIES 11
James R. Ferguson
Acclaimed Contributor

Re: Find Command to search only text files

Hi:

Here's a pure shell approach:

# cat .findit
#!/usr/bin/sh
typeset DIR=$1
typeset PAT=$2
find ${DIR} -xdev -type f | while read FILE
do
[ `file ${FILE} | grep -c ascii` -eq 0 ] && continue
grep "${PAT}" ${FILE} /dev/null
done
exit 0

...run as .findit /path_to_search pattern_to_find

Regards!

...JRF...
Sandman!
Honored Contributor

Re: Find Command to search only text files

Is the string you are searching for a word by itself or is it contained inside another word (a substring) or both?
blal
Frequent Advisor

Re: Find Command to search only text files

hi JRF ,

I already tried type f, it lists jar files etc .

Thanks,
BL
Live and let live.
James R. Ferguson
Acclaimed Contributor

Re: Find Command to search only text files

Hi (again):

> I already tried type f, it lists jar files etc .

Let's not confuse things. Using '-type f' with find limits that which is returned to *files* as opposed to *directories*.

The shell script I suggested skips *binary* files and examines files of type *text*.

Regards!

...JRF...
spex
Honored Contributor

Re: Find Command to search only text files

Hello,

Here's a modified version of JRF's script:

#!/usr/bin/sh
typeset DIR=$1
typeset PAT=$2
find ${DIR} -xdev -type f | while read FILE
do
$(file ${FILE} | cut -d: -f2- | grep -q "text") || continue
grep -l "${PAT}" ${FILE}
done
exit 0

It matches on "text", which means files the 'file' command claims are "ascii text", "awk program text", "c program text", "commands text", "English text", etc. are considered for further investigation by 'grep'. It also filters out the filename from the results of the 'file' command, so that filenames containing "text" do not cause false positives.

PCS
A. Clay Stephenson
Acclaimed Contributor

Re: Find Command to search only text files

Here's a rather fast all Perl Version.

Use it like this:

cd /xxx/yyy/zzz # desired starting directory
findgrep.pl Huey Louie Dewey


It will then start a recursive descent from your CWD looking for regular files that are text files and then checking to see if "Huey", "Louie", or "Dewey" occurs anywhere. If so, the file name is printed. Perl's -T test essentially does everything the file xxx | grep "ascii" does.

If it ain't broke, I can fix that.
blal
Frequent Advisor

Re: Find Command to search only text files

I Tried both the scripts .
there is some thing minor missing in both of these , giving errors .Will you please execute it and see.

Thanks,
BL.
Live and let live.
Patrick Wallek
Honored Contributor

Re: Find Command to search only text files

It would actually be more helpful if you told us how you are executing those and what types of error you are getting.

Dennis Handly
Acclaimed Contributor

Re: Find Command to search only text files

This link may be helpful:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1109119

To solve if Sandman's question is true, you can use grep -w.
Arturo Galbiati
Esteemed Contributor

Re: Find Command to search only text files

Hi,
1. in the current directory without including subdirs:
grep -i word $(file *|grep text|cut -d":" -f1)

2. including subdirs:
grep -i word $(find ./ -type f |xargs file|grep text|cut -d ':' -f1)

this run on hpux 11i.

HTH,
Art
James R. Ferguson
Acclaimed Contributor

Re: Find Command to search only text files

Hi (again):

In the spirit of Perl commandline scripts, this one recursively finds all text files in the current directory and reports case-insensitive matches to the pattern passed as an argument :

# perl -MFile::Find -we 'find(sub{push @f,$File::Find::name if -f $_ && -T _},".");@a=`grep -i $ARGV[0] @f`;print for sort @a'

Regards!

...JRF...