Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am running a grep on a file that is about 13G in size. It is returning

Binary file file.xml matches

I was not expecting this, I thought it would return each line with my string so that I could run the following,

grep "searchString" ./file.xml | wc -l 

and return a count of all the occurrences of my searchString in my large file.

share|improve this question
    
Out of interest, what does file file.xml return? –  EightBitTony Jul 9 '13 at 13:18
    
file.xml: XML document text –  djc391 Jul 9 '13 at 13:22

3 Answers 3

up vote 11 down vote accepted

It looks like grep thinks your XML file is binary rather than text.

If you want to force grep to treat your file as text regardless of the contents, you can use its --text switch (assuming GNU grep), like so:

grep --text "searchString" ./file.xml | wc -l

Note that if all you want is to count matches, it's probably better to use grep --count rather than piping through wc -l, saving you a pipe and process invocation.

share|improve this answer
    
Solved. Thanks! –  djc391 Jul 9 '13 at 13:21
1  
@MichaelKjörling grep --count will also return number of lines. From man: -c, --count Suppress normal output; instead print a count of matching lines for each input file. –  rush Jul 9 '13 at 13:31
    
@rush Argh, you're right. Sorry, never mind that then. –  Michael Kjörling Jul 9 '13 at 13:34

Looks like there are some uncommon symbols in the beginning of your file and grep detects it as binary. You can try --binary-files=text option.

grep --binary-files=text "searchString" file.xml | wc -l

From man page:

   --binary-files=TYPE
          If the first few bytes of a file indicate that the file contains
          binary  data, assume that the file is of type TYPE.  By default,
          TYPE is binary, and grep  normally  outputs  either  a  one-line
          message  saying  that  a  binary  file matches, or no message if
          there is no match.  If TYPE is without-match, grep assumes  that
          a  binary  file  does  not  match;  this is equivalent to the -I
          option.  If TYPE is text, grep processes a binary file as if  it
          were  text;  this is equivalent to the -a option.  Warning: grep
          --binary-files=text might output binary garbage, which can  have
          nasty  side  effects  if  the  output  is  a terminal and if the
          terminal driver interprets some of it as commands.
share|improve this answer
1  
Or near-equivalently grep -a -c 'searchString' file.xml and avoid the superfluous wc -l. –  tripleee Jul 9 '13 at 13:26
    
Just DOS carriage returns are enough to turn a file into "binary". –  tripleee Jul 9 '13 at 13:40
    
@tripleee Not to be a nit, but I thought I already said that :) –  Michael Kjörling Jul 9 '13 at 13:40
    
@MikaelKjörling: After I posted my comment, yes. Yours is now the better answer so you got my vote. –  tripleee Jul 9 '13 at 13:42

It looks like you have a mistake when using ./file.xml. If you try:

grep "searchString" file.xml | wc -l

Does it have any problems?

share|improve this answer
6  
file.xml and ./file.xml refer to exactly the same file, with the additional safety net that in the latter case, the real name of file.xml can safely begin with e.g. -. –  Michael Kjörling Jul 9 '13 at 13:09
1  
not going to vote down but above comment is right no difference between what you have and what I tried. –  djc391 Jul 9 '13 at 13:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.