James Ko
James Ko

Reputation: 34609

Why doesn't grep end with this regexp?

I'm trying to analyze a bunch of C# files I have, with grep, to match a particular type of for-loop. Specifically, anything that looks like the following would match:

for (int i = 0; i < foo.Length; i++)
{
    foo[i] = bar[i];
}

I tried installing pcregrep after I learned grep didn't support multiline, but it's not working out. Here is my (admittedly very sloppy) regexp:

pcregrep -rlM "for (int i = 0; i < [A-Za-z]*\.Length; i++)\n[ *]\{\n[ *][A-Za-z]*\[i\] = [A-Za-z]*\[i\];\n[ *]\}"

I ran this command about 10 minutes ago, and it's still running (albeit at very low CPU usage, surprisingly). Am I doing something wrong / Is there a faster way to do this?

Upvotes: 0

Views: 68

Answers (1)

Ry-
Ry-

Reputation: 225238

pcregrep, unlike grep, doesn’t infer “use the current directory” from -r, so it’s waiting for input, which gives it the appearance of hanging. Specify the path explicitly:

pcregrep -rlM … .

Upvotes: 3

Related Questions