Reputation: 816
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?
Upvotes: 4
Views: 4725
Reputation: 2123
As an additional note, findstr expects CR-LF line endings. If your file uses simple linefeeds, it will not properly process the $
metacharacter.
Upvotes: 3
Reputation: 49096
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.
Upvotes: 2