Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep like syntax with findstr

ive been using grep a lot on linux lately but now i need to use findstr to carry out the same tasks on a windows machine and cant quite get the syntax just right.

My grep command looks like:

grep "12/12/2011.\*followed by literal string" /myFile.txt

so this searches for the date and the literal string specified but can contain a mix of any other characters in between the two search terms by using .\*

Anyone know how to convert this statement to findstr? thanks

like image 396
bobbyrne01 Avatar asked Oct 17 '25 07:10

bobbyrne01


2 Answers

The findstr command supports the /R option to specify the search string as a regular expression. It's the default behavior, however, so you don't actually need to specify it:

findstr "12/12/2011.*followed by literal string" myFile.txt

The above should give the same results as your grep example.

like image 157
Frédéric Hamidi Avatar answered Oct 19 '25 21:10

Frédéric Hamidi


In order to match a literal string, you should use the /C flag, e.g: /C:"Your literal string here" Otherwise a space inside your regex is counted as an OR e.g. this:

findstr "12/12/2011.*followed by literal string" myFile.txt

is the same as

grep 12/12/2011.*followed|by|literal|string myFile.txt

In order to combine both, pipe the output of one into the other, like this:

findstr "12/12/2011.*" myFile.txt | findstr /C:"followed by literal text"
like image 37
Adam Smith Avatar answered Oct 19 '25 23:10

Adam Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!