unix - 两个值之间的Grep字符串Unix
问题描述
我有一个json数据如下。需要 grep 值并将其导出到新文件中。
{
"Data":
[
"User": [
{"Name": "Solomon", "Age":20},
{"Name": "Absolom", "Age":30},
]
"Country": [
{"Name" : "US", "Resident" : "Permanent"},
{"Name" : "UK", "Resident" : "Temporary"}
]]}
预期结果,
需要grep“用户”的值并将其导出到新文件User.json
用户.json
"User": [
{"Name": "Solomon", "Age":20},
{"Name": "Absolom", "Age":30},
]
再次需要 grep "Country" 并导出到 Country.json
国家.json
"Country": [
{"Name" : "US", "Resident" : "Permanent"},
{"Name" : "UK", "Resident" : "Temporary"}
]
正在尝试sed
,但得到的结果不正确
sed -e 's/.*"User"\(.*\)].*/\1/' Data.json > Users.json
由于有很多特殊字符]/[/{/}
,因此不确定如何正确 grep 值并将其导出到新文件。
任何建议都会非常有帮助
解决方案
运气好的话,您可以使用带有-z
选项的 GNU sed 并过滤内容直到关闭]
,假设]
字符串中没有:
sed -z 's/.*\("User":[^]]*]\).*/\1\n/'
sed -z 's/.*\("Country":[^]]*]\).*/\1\n/'
推荐阅读
- android - android 10如何在源代码中禁用随机Mac地址
- python - python - 按第二个列表的子字符串过滤列表
- c# - 如何获取光标下ComboBox Item的索引?
- python - 不能腌制
:它与 builtins.MemoryError 不是同一个对象 - salesforce - Salesforce LiveAgent 域白名单问题
- javascript - 如何在反应组件中使用样式组件
- google-apps-script - 例外:无权访问请求的文档
- python - 在子集总和问题中恢复子集 - 并非所有子集都出现
- spring-boot - org.apache.camel.component.file.GenericFileOperationFailedException:无法存储文件:output\getOrders.txt
- kubernetes - GKE 入口控制器 svc externalTrafficPolicy 到本地