Searching recursively one string on windows OS
Asked Answered
P

2

20

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?

Paine answered 2/8, 2013 at 5:8 Comment(8)
Why this is tagged to C++ ?Warm
@P0W.. Actually I am using C++ files in my project. If it is not worth tag I can remove it.Paine
@Krishna and its already removed !Warm
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.Swoon
possible duplicate of Windows recursive grep command-line. The accepted answer of the question you linked to answers your questions as well.Samantha
@P0W... actually I dont know we have findstr tag. Thanks P0W.. Thanks @MatPaine
@Oswald... Just want to know one thing.. can we provide search strings with space like "int main"? is it valid?Paine
@CaptainObvlious, so every question about the Unix command line is off-topic too? Interesting.Harbinger
H
32

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.

Harbinger answered 2/8, 2013 at 5:23 Comment(1)
why \s instead of \S?Hardwick
L
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.

Lathy answered 30/7, 2015 at 6:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.