4

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

edorian
  • 38,542
  • 15
  • 125
  • 143
bobbyrne01
  • 6,295
  • 19
  • 80
  • 150
  • 3
    You can download grep for Windows: http://gnuwin32.sourceforge.net/packages/grep.htm –  Dec 12 '11 at 11:13
  • ive got a lot of windows machines and id rather make the most of the tools provided by default on windows, rather then installing extra packages on each machine .. thanks for your input though! – bobbyrne01 Dec 12 '11 at 11:20

2 Answers2

5

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.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • But is there a way i can make findstr search the above as a literal search string using a combination of /C and wildcards? I need the resulting line to contain "12/12/2011 \*ANY CHAARACTERS INBETWEEN\* followed by literal string" .. Whereas using regular expressions the results returned will contain mixtures of the literal string found in the myFile.txt – bobbyrne01 Dec 12 '11 at 11:55
  • @Bob, I'm not sure I understand your comment. In my experience, `findstr` will output the exact content of the lines matching the regular expression. Can you elaborate on what you mean by `mixtures of the literal string`? – Frédéric Hamidi Dec 12 '11 at 12:50
  • Yeah sure, the command you gave will return results even if they only contain part of the literal string above i.e. "followed by literal string". So if a line is found which contains the date and the word "followed" that line is returned, i need the command to only return a result if the line contains the entire literal string , "followed by literal string", make sense? – bobbyrne01 Dec 12 '11 at 16:43
  • ive ended up using 'findstr /C:"followed by literal string" myFile* .. then check if any of the results contain the date. Thanks for your input Frederic, ill mark your answer as accepted since its very similiar to what i was looking for – bobbyrne01 Dec 12 '11 at 17:45
1

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"
Adam Smith
  • 52,157
  • 12
  • 73
  • 112