4

I don't get it; the FINDSTR usage is clear, but I can't get it to work. I want to FINDSTR on a text file, using the ^ and the $ character. This way:

findstr /C:"^test$" test.txt

I have made the file (test.txt) in several ways (all on Windows), and there is no trailing space, but still, it doesn't find a matching line.

And it does work without ^ and $.

But it obviously ignores any characters or positions before and after test.

I want to match only lines containing just test and nothing else.

Why does the command not work as expected?

Mofi
  • 46,139
  • 17
  • 80
  • 143
tvCa
  • 796
  • 6
  • 13
  • Are you trying to find a line with only the word "test" on it or are you trying to find lines that start with "test" and/or end with "test"? – apandit Sep 04 '15 at 20:46
  • In the simple example, I'm looking for a line that contains only the word "test". Of course I also want to look for lines with multiple words (and thus: spaces), but if this doesn't work, extended scenario's either won't. – tvCa Sep 04 '15 at 21:04
  • 1
    Try `findstr /r /c:"^test$" test.txt` since you are using a regex. – apandit Sep 04 '15 at 21:10

2 Answers2

3

As an additional note, findstr expects CR-LF line endings. If your file uses simple linefeeds, it will not properly process the $ metacharacter.

Steve Hollasch
  • 2,011
  • 1
  • 20
  • 18
  • After 5 hours of wondering why `findstr` is not working properly, now I know ‍♂️ – kanlukasz Dec 08 '20 at 13:35
  • If this is important, you can try my `eol` tool to convert line endings first and then pipe the result to findstr like so: eol \r\n < README.md | findstr -r -c:"^directory.$" You can find my `eol` tool at https://github.com/hollasch/eol. – Steve Hollasch Dec 09 '20 at 17:36
2

The findstr documentation of Microsoft is indeed not the best. The SS64 documentation for findstr is much better and contains the section Default type of search: Literal vs Regular Expression explaining when FINDSTR runs by default a literal and when a regular expression find if not explicitly defined with /L for literal OR /R for regular expression.

There are several solutions for this task to match only entire lines case-sensitive as by default which can be changed to case-insensitive with /I.

The first one was given already by apandit.

%SystemRoot%\system32\findstr.exe /R /C:"^test$" test.txt

This runs a regular expression find which matches and outputs only lines containing just the word test case-sensitive and nothing else as expected.

Another solution also using a regular expression find is

%SystemRoot%\system32\findstr.exe "^test$" test.txt

Yes, without /C: and because of search string containing the meta-characters ^ and $, this find is executed as regular expression find.

One more solution would be using a literal find which should output only lines matching entirely the specified search string.

%SystemRoot%\system32\findstr.exe /X /C:"test" test.txt

The same result would produce also this literal search.

%SystemRoot%\system32\findstr.exe /B /E /L /C:test test.txt

The parameter /L is not really needed because in this case the find is by default literal. But I think, it is good practice to always specify /L OR /R to make it clear for FINDSTR as well as for the reader of the command which find is executed: a literal or a regular expression find.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • The reason I got confused about this was not understanding that `/R` doesn't clash with `/C:string`, given the help of `/C:string` says, "`/C:string` Uses specified string as a _**literal**_ search string"; if they just dropped the word `literal`, it would have been so much clearer. – kayleeFrye_onDeck Dec 09 '17 at 04:35