What grep command will include the current function name in its output?

前端 未结 9 2176
夕颜
夕颜 2021-01-07 18:08

I run diff with the -p option so the output will include the name of the function where each change occurred. Is there an analogous option for

9条回答
  •  误落风尘
    2021-01-07 18:22

    As with most text processing operations, it's trivial with awk:

    $ awk -v re='return' '/^[[:alpha:]]/{f=FNR"-"$0} $0~re{printf "%s\n%d:%s\n--\n",f,FNR,$0; f="" }' file
    1-int func1(int x, int y)
    3:  return x + y;
    --
    5-int func2(int x, int y, int z)
    9:  return tmp;
    --
    

    The above assumes a function signature is any line that starts with a letter (/^[[:alpha:]]/). If that's not the way your code is written, just tweak to suit.

提交回复
热议问题