根据你提供的示例,你可以尝试以下 awk
程序。这可以通过少量条件而不是使用数组系统来简单完成。
awk '
BEGIN{ OFS="," }
/roleTemplateAppId/{
if(name && template){
print "MAIN_ROLE",name,template,$NF
}
name=template=""
}
/roleTemplateName:/{
template=$NF
next
}
/name:/{
name=$NF
}
END{
if(name && template){
print "MAIN_ROLE",name,template,$NF
}
}
' Input_file
说明:为上述内容添加详细解释。
awk ' ##Starting awk program from here.
BEGIN{ OFS="," } ##Setting OFS to , in BEGIN section.
/roleTemplateAppId/{ ##Check if line contains roleTemplateAppId then do following.
if(name && template){ ##check if name and template is SET then do following.
print "MAIN_ROLE",name,template,$NF ##Printing MAINE_ROLE name, template and last field value here.
}
name=template="" ##Nullifying name and template here.
}
/roleTemplateName:/{ ##Check if roleTemplateName: is found in current line then do following.
template=$NF ##Setting template to last field of current line.
next ##next will skip all further statements from here.
}
/name:/{ ##Checking condition if line contains name: then do following.
name=$NF ##Setting name value as last field name.
}
END{ ##Starting END block of this program from here.
if(name && template){ ##check if name and template is SET then do following.
print "MAIN_ROLE",name,template,$NF ##Printing MAINE_ROLE name, template and last field value here.
}
}
' Input_file
sed
解析 JSON。 - chepner