1830939 Members
2764 Online
110017 Solutions
New Discussion

grep file problem

 
nash11
Frequent Advisor

grep file problem

when I use grep ( eg. grep -i -r "string" * ) , it will stop the process when hitting a binary file ( the error is "Binary file ./bin/szxtu matches" ) , could advise how to ignore the binary file to grep , or suggest how to use grep if there are some binary file is the system ? thx.
6 REPLIES 6
H.Merijn Brand (procura
Honored Contributor

Re: grep file problem

GNU grep will grep binary files only when passing the -a option. As GNU grep is so much better than the default grep, I cannot remember how long ago it was I used the default grep.

http://hpux.connect.org.uk/hppd/hpux/Gnu/grep-2.5.1a/

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
nash11
Frequent Advisor

Re: grep file problem

thx reply ,

Actually , I use other distro. like RH and AIX , could advise how to know what kind grep ( GNU / default ) is using ? thx
H.Merijn Brand (procura
Honored Contributor

Re: grep file problem

a5:/u/usr/merijn 102 > grep --version
grep (GNU grep) 2.5.1

Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

a5:/u/usr/merijn 103 > /usr/bin/grep --version
/usr/bin/grep: illegal option -- -
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] -e pattern_list...
[-f pattern_file...] [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] [-e pattern_list...]
-f pattern_file... [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] pattern [file...]
Exit 2
a5:/u/usr/merijn 104 >

FWIW I use GNU grep also on AIX
I always compile it myself, so I can include -P for pcre patterns

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Arturo Galbiati
Esteemed Contributor

Re: grep file problem

Hi,
without install GNU grep you can use:

# 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
Sandman!
Honored Contributor

Re: grep file problem

Use the find command in order to skip the binary files since UNIX sees only a byte stream.

# find -type f -exec grep -ir "string_to_match" {}\;

~hope it helps
Bill Hassell
Honored Contributor

Re: grep file problem

It is almost impossible to skip binary files. The reason is that all files are binary, but some contain ASCII strings which humans call text files. In Unix, there are no just a few file types (regular, device, pipe, etc) but a binary file is a regular file. The only help is the filetype guessing program called "file". It uses a magic file (/egtc/magic) to compare well-known data patterns such as executables and libraries. However, a file like /etc/lvmtab contains both ASCII strings as well as binary data, so the file command reports "data". Unfortunately, file does report just "data" or "text" as there are different flavors for these files.

So the best solution is not to search everywhere for ASCII file strings. You won't find ASCII files in /usr/share/man/cat* or /usr/bin, so those directgories should be excluded. Search in directories where there is a reasonable chance the needed files will be found.


Bill Hassell, sysadmin