0

I have Python code from which I want to extract HTTP parameters.

In the following: how can I leave the def lines alone, but from remaining lines preserve only what is within params['...'] perhaps with multiple params['...'] on one line:

Input:

def delete_bazooka (request):
    BType.objects.get(id=params['id']).delete()
    BType.objects.get(name=params['name']).delete()
def create_bazooka (request):
  s = Skill.objects.get(id=params['skill'])
  l = Level.objects.get(id=params['level'])
  bt = BType.objects.get(name=params['type'])
  b = Bazooka(type=qt, bin=b, bin_sequence=b.count, text=params['text'], explanation=params['chocolate'], passage=p)
def delete_hoop (request):
  h = Hoop.objects.get(id=params['id'])

Desired Output:

def delete_bazooka (request):
id
name
def create_bazooka (request):
skill
level
type
text chocolate
def delete_hoop (request):
id
2
  • 1
    you mean you want to print the value of params keys? That would be simply: print params["id"], or if there are multiple ones: for key in params: print params[key] Commented Sep 19, 2013 at 6:13
  • @Pawelmhm no, i am analyzing python code using awk/sed. i cannot run it. i want to document it. Commented Sep 19, 2013 at 6:15

2 Answers 2

3

You can use grep `with the E (regexp) and o (only matching) flags:

grep -Eo "(?def .*|'[a-z]+')" test.txt
Sign up to request clarification or add additional context in comments.

Comments

1

Using awk

awk -F\' '{$0=/^def/?$0:$2" "$4}1' test.txt

1 Comment

this hardcodes for 1 or 2 but fails for n params on 1 line; the other answer chosen as correct works for n. upvoted regardless because my id is no_answer_not_upvoted ;-)

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.