[Solved] search a string only inside a function definetion


Of course, try this:

$0 ~ fun {
    count = 1
    while (! ($0 ~ /{/))
        getline
    getline
}   

count > 0 {
    if ($0 ~ /{/)
        count++
    if ($0 ~ /}/)
        count--
    if ($0 ~ query)
        print FILENAME ": l" FNR ". " $0
}

And invoke the script like this:

awk -v query="pankaj" -v fun="void f[(]" -f script.awk inputfile.java

Where query is the string to search and fun the regex for the function name.

This script counts { and } to see when we leave the function and should print the line if a match is found.

Edit: you may want to extend the regex for counting brackets, perhaps an extra check to see if they aren’t placed in comments is required (although you’d never do that).

6

solved search a string only inside a function definetion