CygWin: grep command shows some “binary file coincidence” message

cygwin;grep

I have been googling about this, but, maybe due to my spanish version of CygWin/Windows, I have not been able to find a solution.
I have latest (December 2014) CygWin 32 bits installed, and it seems there is some problem with its grep command.

As you know, CygWin shell can run both Windows and Linux commands, so I am trying to filter the results of my services list command, with this results:

$ sc query state= all | grep "stopped" -i
Coincidencia en el fichero binario (entrada estándar)

I think this message translates to: Binary file match (standard input).

If I try from a Windows shell (using only Windows commands):

C:\Users\Luis-> sc query state= all | find "stopped" /i 
        ESTADO                       : 1  STOPPED
        ESTADO                       : 1  STOPPED
        ESTADO                       : 1  STOPPED
        ESTADO                       : 1  STOPPED
        ESTADO                       : 1  STOPPED

… I obtain the correct results.

This is the CygWin embedded grep version:

$ grep -V
grep (GNU grep) 2.21
Copyright (C) 2014 Free Software Foundation, Inc.
Licencia GPLv3+: GPL de GNU versión 3 o posterior

I have found some sort of workaround by downloading UnxUtils and using its grep 32bit-Windows version:

$ PathToUNXUtils="/cygdrive/d/Utilidades/UnxUtils/"

$ "$PathToUNXUtils"grep -V
grep (GNU grep) 2.5.1
Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc.

$ sc query state= all | "$PathToUNXUtils"grep "stopped" -i | more.com
        ESTADO                       : 1  STOPPED
        ESTADO                       : 1  STOPPED
        ESTADO                       : 1  STOPPED
        ESTADO                       : 1  STOPPED
        ESTADO                       : 1  STOPPED
        ESTADO                       : 1  STOPPED

But, well… I think the appropriate way to solve this would be to make the original CygWin's grep version work as it must.

What is going on here? Should I download and compile the latest grep version as last hope?

Best Answer

How to grep a text file which contains some binary data?

Syntax

grep [options] PATTERN [FILE...]

grep [options] [-e PATTERN | -f FILE] [FILE...]

Options

...

-a

--text

Process a binary file as if it were text; this is equivalent to the --binary-files=text option.

Using the above option will allow grep to ignore any strange characters that prevent grep working as expected.

So the following should work:

sc query state= all | grep "stopped" -i --text

Or:

sc query state= all | grep "stopped" -a -i

Source StackOverflow answer How to grep a text file which contains some binary data? by paxdiablo and grep - Search file(s) for specific text.

Related Question