1

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?

4
  • Hmm. sc query state= all | grep "stopped" -i works as expected here. Lastest version of cygwin (but 64 bit).
    – DavidPostill
    Dec 9, 2014 at 11:39
  • Yep, @DavidPostill, it works for me too sometimes. Seem to be failing only often. Dec 9, 2014 at 15:26
  • Possible duplicate of How to grep a text file which contains some binary data?
    – DavidPostill
    Dec 9, 2014 at 15:37
  • 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. Dec 9, 2014 at 16:05

1 Answer 1

6

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.

1
  • 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. Dec 9, 2014 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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