CygWin: grep command shows some "binary file coincidence" message

1

2

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?

Sopalajo de Arrierez

Posted 2014-12-09T03:54:05.990

Reputation: 5 328

Hmm. sc query state= all | grep "stopped" -i works as expected here. Lastest version of cygwin (but 64 bit). – DavidPostill – 2014-12-09T11:39:19.350

Yep, @DavidPostill, it works for me too sometimes. Seem to be failing only often. – Sopalajo de Arrierez – 2014-12-09T15:26:11.983

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

– DavidPostill – 2014-12-09T15:37:32.627

You have pointed the problem exactly, @DavidPostill. And, so, the solution. As seen in that thread, there are several ways to solve it. Just adding --text to grep could enough in some cases, but i should be better to analyze it. You can post it as an answer if you want. If not, I will probably write it later myself. Thanks you. – Sopalajo de Arrierez – 2014-12-09T16:05:51.023

Answers

5

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.

DavidPostill

Posted 2014-12-09T03:54:05.990

Reputation: 118 938

Can we suppose that some commands like "sc query" drop binary non-printable data when invoked from any CygWin shell? That could explain the issue. – Sopalajo de Arrierez – 2014-12-09T20:27:07.510