FINDSTR – Windows CMD Command


Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/future4tech/public_html/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

Search for a text string in a file (or multiple files) unlike the simple FIND command FINDSTR supports more complex regular expressions.

When using /G or /F the text file should be a plain ANSI text file with one Search string or Filename/Path on each line.
If more than one file is searched (/F), the results will be prefixed with the filename where the text was found.

The default behaviour of FINDSTR is to match any word, so FINDSTR “blue planet” will match the word blue or the word planet.
To match an entire phrase/sentence or to use Regular Expressions use the /C and /R options.

Option syntax

Options can be prefixed with either / or –
Options can also be concatenated after a single / or -. However, the concatenated option list can contain at most one multicharacter option such as OFF or F:, and the multi-character option must be the last option in the list.

The .* expression can be useful within a larger expression, for example a.*b will match any string beginning with A and ending with B.

FINDSTR does not support alternation with the pipe character (|) multiple Regular Expressions can be separated with spaces, just the same as separating multiple words (assuming you have not specified a literal search with /C) but this might not be useful if the regex itself contains spaces.

FINDSTR does not support UTF-16 files, but FIND does.

Regex Line Position anchors ^ and $

^ matches beginning of input stream as well as any position immediately following a <LF>. Since FINDSTR also breaks lines after <LF>, a simple regex of “^” will always match all lines within a file, even a binary file.
So to find the string snark at the beginning of a line, use ^snark

$ matches any position immediately preceding a <CR>. This means that a regex search string containing $ will never match any lines within a Unix style text file, nor will it match the last line of a Windows text file if it is missing the EOL marker of <CR><LF>.
So to find the string snark at the end of a line, use snark$

Note – As detailed further below, piped and redirected input to FINDSTR can have <CR><LF> appended that is not in the source. This can impact a regex search that uses $.

A search string with characters (before ^) OR (after $) will always fail to find a match. $nevermatches^

Positional Options /B /E /X

The positional options work the same as ^ and $, except they also work for literal search strings.

/B functions the same as ^ at the start of a regex search string.

/E functions the same as $ at the end of a regex search string.

/X functions the same as having both ^ at the beginning and $ at the end of a regex search string.

FINDSTR – Escapes and Length limits – More detail of how to use search strings that include quotes and/or backslashes. Also maximum Search String length limits vary with OS version.

FINDSTR – Searching across line breaks

Regex character class ranges [x-y]

Character class ranges do not work as expected. See this Q/A on Stack Exchange: Why does findstr not handle case properly (in some circumstances)?

The problem is FINDSTR does not collate the characters by their byte code value (commonly thought of as the ASCII code, but ASCII is only defined from 0x00 – 0x7F). Most regex implementations would treat [A-Z] as all upper case English capital letters. But FINDSTR uses a collation sequence that roughly corresponds to how SORT works. So [A-Z] includes the complete English alphabet, both upper and lower case (except for “a”), as well as non-English alpha characters with diacriticals.

The FINDSTR regex sorts lower case before upper case. So findstr /nrc:”^[A-a]” will find nothing, but findstr /nrc:”^[a-A]” will match.

Default type of search: Literal vs Regular Expression

/C:”string” – The default match like /L literal, but will also accept spaces.

/R /C:”Search string” – This will perform a Regex match, but will also accept spaces in the search string.

“string argument” – The default depends on the content of the very first search string. (Remember that <space> is used to delimit search strings.) If the first search string is a valid regular expression that contains at least one un-escaped meta-character, then all search strings are treated as regular expressions. Otherwise all search strings are treated as literals. For example, “51.4 200” will be treated as two regular expressions because the first string contains an un-escaped dot, whereas “200 51.4” will be treated as two literals because the first string does not contain any meta-characters.

/G:file – The default depends on the content of the first non-empty line in the file. If the first search string is a valid regular expression that contains at least one un-escaped meta-character, then all search strings are treated as regular expressions. Otherwise all search strings are treated as literals.

Recommendation – Always explicitly specify /L literal option or /R regular expression option when using “string argument” or /G:file.

Searching for Spaces

When the search string contains multiple words, separated with spaces, then FINDSTR will return lines that contain either word (OR).
A literal search (/C:”string”) will reverse this behaviour and allow searching for a phrase or sentence. A literal search also allow searching for punctuation characters.

where

fileName = The name of the file containing the matching line. The file name is not printed if the request was explicitly for a single file, or if searching piped input or redirected input. When printed, the fileName will always include any path information provided. Additional path information will be added if the /S option is used. The printed path is always relative to the provided path, or relative to the current directory if none provided.

lineNumber = The line number of the matching line represented as a decimal value with 1 representing the 1st line of the input. Only printed if /N option is specified.

lineOffset = The decimal byte offset of the start of the matching line, with 0 representing the 1st character of the 1st line. Only printed if /O option is specified.

text = The binary representation of the matching line, including any <CR> and/or <LF>.

Nothing is left out of the binary output, such that this example that matches all lines will produce an exact binary copy of the original file:

Using a script file

Multiple search criteria can be specified with a script file /G
Multiple FileNames to search can be specified with /F

When preparing a source or script file, place each filename or search criteria on a new line.
If several filenames are to be searched they must all exist or FINDSTR will fail with an error.

Piping and Redirection

A text file can be piped or redirected into FINDSTR:

  • Data stream from a pipe TYPE file.txt | FINDSTR “searchString”
  • Stdin via redirection FINDSTR “searchString” <file.txt

The various data source specifications are mutually exclusive – FINDSTR can only work with one of the following: filename argument(s), /F: file option, redirected input, or piped input.

Piped and Redirected input can have <CR><LF> appended:

  • If the input is piped in and the last character of the stream is not <LF>, then FINDSTR will automatically append <CR><LF> to the input. (XP, Vista and Windows 7.)
  • If the input is redirected and the last character of the file is not <LF>, then FINDSTR will automatically append <CR><LF> to the input. (Vista only), Note that in this case XP and Windows 7/2008 will not alter redirected input which can cause FINDSTR to hang indefinitely.

Errorlevel

FINDSTR will set %ERRORLEVEL% as follows:

0 (False) a match is found in at least one line of at least one file.
1 (True) if a match is not found in any line of any file, (or if the file is not found at all).
2 Wrong syntax
An invalid switch will only print an error message in the error stream.

Bugs

If the last character of a file used as redirected input does not end with <LF>, then FINDSTR will hang indefinitely once it reaches the end of the redirected file.

FINDSTR cannot search for null bytes commonly found in Unicode files.

Based on experiments, FINDSTR can fail if all of the following conditions are met:

  • The search is using multiple literal search strings
  • The search strings are of different lengths
  • A short search string has some amount of overlap with a longer search string
  • The search is case sensitive (no /I option)

It seems to always be the shorter search strings that fail, for more info see: FINDSTR fails to match multiple literal search strings

In early versions of FindStr /F:file a path length of more than 80 chars will be truncated.

You may also like...

17 Responses

  1. 0mniartist says:

    I know this web site presents quality dependent articles or reviews and additional
    data, is there any other web site which provides these data in quality?
    asmr 0mniartist

  2. 0mniartist says:

    What a information of un-ambiguity and preserveness of precious knowledge concerning unpredicted emotions.
    0mniartist asmr

  3. 0mniartist says:

    I’m impressed, I have to admit. Rarely do I come across a
    blog that’s equally educative and engaging, and let me tell you, you’ve hit the nail on the head.
    The issue is something too few men and women are speaking intelligently
    about. I’m very happy that I found this in my search for something regarding this.
    0mniartist asmr

  4. 0mniartist says:

    It’s going to be finish of mine day, but before ending I am reading this enormous paragraph to increase
    my experience. 0mniartist asmr

  5. 0mniartist says:

    great points altogether, you simply received a logo new reader.

    What could you suggest in regards to your put up that
    you simply made some days in the past? Any positive?
    0mniartist asmr

  6. travelers notebook wallet insert says:

    Fantastic post but I was wondering if you could write a
    litte more on this topic? I’d be very thankful if you could
    elaborate a little bit more. Bless you!

  7. froleprotrem says:

    Sweet site, super design, real clean and apply genial.

  8. cheap flights says:

    Its like you read my thoughts! You seem to know so much about this, such
    as you wrote the e book in it or something. I feel
    that you simply could do with some p.c. to pressure the message home
    a little bit, however instead of that, this is excellent blog.
    An excellent read. I will certainly be back.

  9. cheap flights says:

    Thanks for every other informative website. The place else
    may I get that type of information written in such an ideal way?

    I have a mission that I’m just now operating on, and I’ve been at the glance out for
    such info.

  10. cheap flights says:

    This piece of writing is genuinely a good one it assists new internet viewers, who are
    wishing for blogging.

  11. view it electric company near me says:

    Good site! I really love how it is simple on my eyes and the data are well written. I’m wondering how I could be notified whenever a new post has been made. I have subscribed to your feed which must do the trick! Have a great day!

  12. lamborghini huracan says:

    Thanks for sharing your thoughts on btc price. Regards

  13. Source electrical repair says:

    I was just seeking this information for some time. After 6 hours of continuous Googleing, at last I got it in your web site. I wonder what is the lack of Google strategy that don’t rank this type of informative websites in top of the list. Generally the top websites are full of garbage.

  14. visit this site electrical contractor says:

    Thanks for the article, can you make it so I get an email when there is a fresh article?

  15. emergency plumber near me view it says:

    Well I definitely liked studying it. This article procured by you is very useful for accurate planning.

  16. emergency plumber near me this hyperlink says:

    Somebody essentially help to make seriously posts I would state. This is the very first time I frequented your web page and thus far? I surprised with the research you made to make this particular publish incredible. Great job!

  17. best plumbing browse around here says:

    Well I definitely enjoyed studying it. This post offered by you is very useful for good planning.

Leave a Reply

Your email address will not be published. Required fields are marked *