Operating System - HP-UX
1754376 Members
3220 Online
108813 Solutions
New Discussion юеВ

Advanced find of text in files with formatted output

 
James Beamish-White_1
Occasional Advisor

Advanced find of text in files with formatted output

Hiya,

I want to search a directory structure for files that contain a text pattern, and for each line containing that pattern print out the name of the file and the line containing the text. I have found this perl snippet:

perl -MFile::Find -le$/=undef;find(sub{-f&&-T or return;local@ARGV=($_);<>=~/pattern/ and print$File::Find::name},"/opt/")

which is great for finding the pattern, but I can't figure out how to print the line of text as well. I also guess that this could be done with awk, but I'm not that great with awk.

Thanks!
James
10 REPLIES 10
James Beamish-White_1
Occasional Advisor

Re: Advanced find of text in files with formatted output

figured it out about 2 minutes after posting this:

perl -MFile::Find -le$/=undef;find(sub{-f&&-T or return;local@ARGV=($_);<>=~/(.*pattern.*)/ and print$File::Find::name . " : " . $1},"/opt/")
Pete Randall
Outstanding Contributor

Re: Advanced find of text in files with formatted output

James,

Unless you want to stick to perl, I think find and grep would do what you want:

"find /example /new/example -exec grep 'Where are you' {} \;" (from the man page, without the -l)


Pete


Pete
James Beamish-White_1
Occasional Advisor

Re: Advanced find of text in files with formatted output

Not quite. It does not satisfy the "for each line containing that pattern print out the name of the file and the line containing the text". So the output needs to be something like:

/home/user/file.txt : line containing pattern

Tried to do it simply in scripts, but couldn't figure it out...
Sridhar Bhaskarla
Honored Contributor

Re: Advanced find of text in files with formatted output

Hi James,

grep isn't a great tool to 'grep' for strings. Just for the heck of it, xargs with find may give you what you want.

find /somewhere -type f |xargs grep "string"


-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Jose Mosquera
Honored Contributor

Re: Advanced find of text in files with formatted output

Hi,

#find -type f -exec grep '' {} \; -print

To add line number where string was found:
#find -type f -exec grep -n '' {} \; -print

Rgds.
Muthukumar_5
Honored Contributor

Re: Advanced find of text in files with formatted output

We can get file number and filenames when searching on mulitple files as,

grep -n 'pattern' `find -type f -name "filename-regexpattern"`

Note: Some files are being object files so that grep on that file will make unneed results there on session there.

Check the file type with file command.

Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Advanced find of text in files with formatted output

We can do search more effective with script as,
#!/usr/bin/sh
# test.sh
pth="$1"
pattern="$2"


for file in `find $pth -type f name "*"`
do

if [[ $(file $file | grep -q "executable") -ne 0 ]]
then
grep -n "$pattern" $file
fi

done
Easy to suggest when don't know about the problem!
Steve Post
Trusted Contributor

Re: Advanced find of text in files with formatted output

If you find the string in a binary file, I don't think you'll want to look at it. Your screen will mess up. This method will find only text files, then look through them. It is a manual procedure here. But I like it that way. This came from the forums.

1. cd MYDIRECTORY
2. find . -type f -print > bigfile
3. vi bigfile
remove logs, proxy pages, other bogus areas you don't want to bother looking at. Remove any OUTPUT FILE or you'll have an infinite loop.
4. cat bigfile | \
while read name;do
case "$(file $name)" in
*text* ) grep -Ei "string" $name 2>>/dev/null ;;
esac
done

Re: Advanced find of text in files with formatted output

Assuming you dont have too big a list of contender files:
grep "pattern" $(find path -type f | perl -lne 'print if -T;' | xargs grep -l "pattern")

NOTE: path is the top of the subtree you want
to consider

(the perl subscript will consider only text files)