Grep with unknown pattern?

Hi.

I have a small problem that I haven't been able to find out of on my own. I am not much into bash scripting, however I use grep now and then when working on my code in order to locate specific objects, so I'll just state my exact problem:

The code is huge, and has a function that is simply named "f", I want to see every instance where it is being used. It has two arguments that I don't care about, so my first attempt was to do the following:

grep "f(*,*)" */*f90

This then gave me every line in the code that ended with "f)" as well as every time we had an "f(" (meaning every if test and a billion other routines).

I'm therefore searching for a way to let grep only search for the pattern where f is followed by a left paranthesis, with two arguments I don't care about and then a right parenthesis.

Additional help would be to tell grep to ignore all the f's that have a letter preceeding it (this would let me not get all the if tests, but would also give me the instances when f is in a product (ala "*f").

Thanks in advance to any who could help me.

Something like this may work:

grep -E '\<f\([^),]+,[^),]+\)' */*f90

-E - for extended regular expressions (the + quantifier is part of the EREs)
\< - GNU word boundary - \< matches at the start of a word, note that some grep implementations support the \b syntax.
[^...] - negated character class - match everything but the characters in the list - in this case everything that's not ) or , .
+ - one or more of the preceding pattern - one or more character that's neither ) nor , .

Hope this helps.

1 Like

Thanks, that worked like a charm.

And doubly thanks for explaining all the terms rather than giving me a solution (that looks really complicated at first glance).