0

I have tried:

$ grep -Po "def[[:space:]]+\K(.*)\(" code.py

The output should be a list of function names.

I think it should be correct, but it's only capturing one extra bracket. I would like to get rid off it in the expression (not postprocessing). Please, explain the syntax, you will use.

9
  • 1
    add a sample input and output... I think grep -Po 'def[[:space:]]+\K[^(]+' code.py is what you need Commented Apr 30, 2017 at 5:14
  • You realize this is missing any arguments to the method, right? So do you just want the function name or the entire definition name(args) ? Commented Apr 30, 2017 at 5:14
  • It seems like the ast module might be a better bet for what you're trying to do. Commented Apr 30, 2017 at 5:15
  • @pvg I know it exists, but for me quick grep is sufficient. I practise grep. Commented Apr 30, 2017 at 5:18
  • @xralf I get that but... There's a thing built into python that reliably parses python. Alternatively, you can look at what [whatnot]tags or your favourite python syntax highlighter has for python regexes. Commented Apr 30, 2017 at 5:20

1 Answer 1

1

You can omit the "(" from the resulting string by putting it in a lookahead (?=<expression>):

grep -Po "def[[:space:]]+\K(.*)(?=\()" code.py
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.