使用shell命令在花括号之间Grep多行



我想从文件中删除花括号之间的多行。文件originalfile.txt的一个例子可以是:

{
"User ID": "admin",
"Tower Job ID": "",
"Date": "Fri Jan 15 12:50:13 UTC 2021",
"Playbook Input": [
{"Host FQDN": "abc.cloud"},
{"Host IP Address": "10.10.10.1"},
{"Host Serial Number": "ABCD"},
{"RITM Ticket": "sk_123"}
],
"Playbook Output": {
"Results" : [
{ "Update server" : "Pass" , "Result" : "done" },
{ "Decom server" : "Pass" , "Result" : "done" },
{ "Check process" : "Pass" , "Result" : "done" },
{ "Schedule" : "Pass" , "Result" : "Scheduled @ 2021-01-15 04:53:55" },
{ "Playbook summary" : "Pass" , "Result" : "Complete" }
]
}
}

我想只grepPlaybook Output之后的行,如:

{
"Results" : [
{ "Update server" : "Pass" , "Result" : "done" },
{ "Decom server" : "Pass" , "Result" : "done" },
{ "Check process" : "Pass" , "Result" : "done" },
{ "Schedule" : "Pass" , "Result" : "Scheduled @ 2021-01-15 04:53:55" },
{ "Playbook summary" : "Pass" , "Result" : "Complete" }
]
}

是否有一种方法可以从文件中获取大括号之间的特定内容?

您的输入结构为JSON,因此您可以使用jq shell工具对其进行过滤

$ jq '."Playbook Output"' foo.json
{
"Results": [
{
"Update server": "Pass",
"Result": "done"
},
{
"Decom server": "Pass",
"Result": "done"
},
{
"Check process": "Pass",
"Result": "done"
},
{
"Schedule": "Pass",
"Result": "Scheduled @ 2021-01-15 04:53:55"
},
{
"Playbook summary": "Pass",
"Result": "Complete"
}
]
}
sed -n -e '/Results/,$p' yourfile.dat

如果你想保留上面的大括号:

sed -n -e '/Results/,$p' yourfile.dat | sed -e '1 i {'

输出:

{
"Results" : [
{ "Update server" : "Pass" , "Result" : "done" },
{ "Decom server" : "Pass" , "Result" : "done" },
{ "Check process" : "Pass" , "Result" : "done" },
{ "Schedule" : "Pass" , "Result" : "Scheduled @ 2021-01-15 04:53:55" },
{ "Playbook summary" : "Pass" , "Result" : "Complete" }
]
}
}

相关内容

  • 没有找到相关文章

最新更新