解析字段的脚本

解析字段的脚本

我需要解析文件并提取某些字段:

示例文件格式:

"make":"honda","group":"xyz","model":"accord","year":"2007","miles":"67000"
"make":"honda","group":"abc","model":"civic","year":"2011","miles":"40000"
"make":"toyota","group":"abc","model":"camry","year":"2011","miles":"40000"
"make":"toyota","color":"black","group":"hgh","model":"camry","year":"2015","miles":"2000"
"make":"lincoln","color":"silver","group":"ugh","model":"MKZ","year":"2013","miles":"20000"

我需要提取模型和里程下的字段并写入一个单独的文件。从上面,我需要如下摘录:

accord:67000
civic:40000
camry:40000
camry:2000
MKZ:20000

答案1

使用awk:

awk -F'[:,]' '{gsub(/"/,""); printf "%s:%s\n", $(NF-4),$NF}' file
accord:67000
civic:40000
camry:40000
camry:2000
MKZ:20000

相关内容