Grep equivalent for Windows (String Parsing)

The built in windows command FindStr mirrors the capabilities of the Unix command Grep.

Findstr /?
FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file]
        [/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]]
        strings [[drive:][path]filename[ …]]
  /B         Matches pattern if at the beginning of a line.
  /E         Matches pattern if at the end of a line.
  /L         Uses search strings literally.
  /R         Uses search strings as regular expressions.
  /S         Searches for matching files in the current directory and all
  /I         Specifies that the search is not to be case-sensitive.
  /X         Prints lines that match exactly.
  /V         Prints only lines that do not contain a match.
  /N         Prints the line number before each line that matches.
  /M         Prints only the filename if a file contains a match.
  /O         Prints character offset before each matching line.
  /P         Skip files with non-printable characters.
  /OFF[LINE] Do not skip files with offline attribute set.
  /A:attr    Specifies color attribute with two hex digits. See “color /?”
  /F:file    Reads file list from the specified file(/ stands for console).
  /C:string  Uses specified string as a literal search string.
  /G:file    Gets search strings from the specified file(/ stands for console).
  /D:dir     Search a semicolon delimited list of directories
  strings    Text to be searched for.
  [drive:][path]filename Specifies a file or files to search.

Example of using Findstr to search for multiple strings in succession within a log file.
findstr “ERROR Caused Fail” c:logslog.log

This command will search for the String ERROR then Caused, and Fail while parsing down a file. So if the file looked like this:
Line1: Error Script Failed
Line2: test1
Line4: Caused by process down
Line5: test2
Line6: Error2

The FindStr output would look like this:
Error Script Failed
Caused by Process down

26 thoughts on “Grep equivalent for Windows (String Parsing)

  1. This is great; works well with typical unix commands and a pipe. Eg or ¦ findstr “findme”. Thanks for the tip!

  2. Exactly what I was looking for, trying to go from Unix shell scripting to Windows is a little frustrating! Thanks!

  3. Pingback: Grep Equivalent for Windows « BasementJack

  4. I did not know about this, and you saved me tons of time I had otherwise spent installing and trying to work with a dozen other tools! Many thanks.

  5. Findstr is (and always has been) full of well documented bugs. Especially if you’re trying to search for multiple strings at the same time. If you can’t get your script to work the way it should, I recommend you Google “findstr bugs” to find out all its problems. There are work-arounds, but none are 100%.

  6. Is there any way I can find out the value of a specific column from a command output??
    For example, I have a tab separated values as follows resulting from a command:

    C:\> nco_pa_status -server SCOM_P_PA -user ‘test-user’ -password ‘xxxxxxxxx’

    Probes SCOM2012Probe LION-01 0 RUNNING 2344

    Is there anyway, I can grep the value of column 5? In this example, the value of column 5 is “RUNNING”. The alternative values of this column would be “DEAD” or “PENDING”.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.