19

I want to search one string e.g. "main" in my project on windows OS recursively. I searched that and find a solution Windows recursive grep command-line

I applied same with two different approach, and result is not as expected.

e.g. my approach

findstr /S "main" *.cpp

but when I choose

findstr /S "int main" *.cpp

I am not getting only my main function. What is the difference between these two approaches? is it wrong to provide strings with space?

Community
  • 1
  • 1
someone
  • 1,638
  • 3
  • 21
  • 36
  • 2
    Why this is tagged to `C++` ? – P0W Aug 02 '13 at 05:11
  • 1
    @P0W.. Actually I am using C++ files in my project. If it is not worth tag I can remove it. – someone Aug 02 '13 at 05:12
  • @Krishna and its already removed ! – P0W Aug 02 '13 at 05:13
  • 1
    This is off topic. `findstr` is not _directly_ related to programming and the fact that you are searching source files is IMHO irrelevant. This question would be better served on SuperUser. – Captain Obvlious Aug 02 '13 at 05:13
  • possible duplicate of [Windows recursive grep command-line](http://stackoverflow.com/questions/698038/windows-recursive-grep-command-line). The accepted answer of the question you linked to answers your questions as well. – Oswald Aug 02 '13 at 05:13
  • @P0W... actually I dont know we have `findstr` tag. Thanks P0W.. Thanks @Mat – someone Aug 02 '13 at 05:14
  • @Oswald... Just want to know one thing.. can we provide search strings with space like "int main"? is it valid? – someone Aug 02 '13 at 05:16
  • 3
    @CaptainObvlious, so every question about the Unix command line is off-topic too? Interesting. – Joey Aug 02 '13 at 05:22

2 Answers2

33

This is because findstr takes a set of strings to search for. To actually match the string int main you have to use the /C option:

findstr /s /C:"int main" *.cpp

whereas your variant gives you every line with either int or main.

Joey
  • 344,408
  • 85
  • 689
  • 683
4

Kind of late to the party here, but if you use

findstr /S "int.main" *.cpp

it will treat the dot as a wild card, which matches a space, and as long as you don't mind some superfluous matches (which are unlikely in this case) it will work fine for you.

I use that, having not known about the /C: option before reading the answers above.

T G
  • 445
  • 3
  • 7