使用awk / sed脚本来提取函数参数

时间:2013-09-19 06:07:54

标签: python bash sed awk

我有Python代码,我想从中提取HTTP参数。

以下内容:如何单独留出def行,但是剩下的行只保留params['...']内可能包含多行params['...']的行:

输入:

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'])

期望的输出:

def delete_bazooka (request):
id
name
def create_bazooka (request):
skill
level
type
text chocolate
def delete_hoop (request):
id

2 个答案:

答案 0 :(得分:3)

您可以将grep`与E(regexp)和o(仅匹配)标志一起使用:

grep -Eo "(?def .*|'[a-z]+')" test.txt

答案 1 :(得分:1)

使用awk

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