Bash Shell 脚本提取 Json 对象 [英] Bash Shell script extract Json object

查看:31
本文介绍了Bash Shell 脚本提取 Json 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Bash shell 脚本中,我想提取一个对象.例如,使用以下 json 文件,我想提取 dependencies 对象,它应该返回我:"dmg": ">= 0.0.0", "build-essential":">= 0.0.0", "windows": ">= 0.0.0" 任何格式,你怎么做?

In Bash shell script, I want to extract an object. For example, with following json file, I would like to extract dependencies object and it should return me: "dmg": ">= 0.0.0", "build-essential": ">= 0.0.0", "windows": ">= 0.0.0" in whatever format and how do you do that?

//我的数据1.json:

// My data 1.json:

{
    "platforms": {
        "amazon": ">= 0.0.0",
        "arch": ">= 0.0.0",
        "centos": ">= 0.0.0",
        "debian": ">= 0.0.0"
    },
    "dependencies": {
        "dmg": ">= 0.0.0",
        "build-essential": ">= 0.0.0",
        "windows": ">= 0.0.0"
    },
    "recommendations": {}
}

//我的数据2.json:

// My data 2.json:

{
    "platforms": {
        "amazon": ">= 0.0.0",
        "arch": ">= 0.0.0",
        "centos": ">= 0.0.0",
        "debian": ">= 0.0.0"
    },
    "recommendations": {},
    "dependencies": {
        "dmg": ">= 0.0.0",
        "build-essential": ">= 0.0.0",
        "windows": ">= 0.0.0"
    }
}

//我的数据3.json:

// My data 3.json:

{
    "dependencies": {
        "dmg": ">= 0.0.0",
        "build-essential": ">= 0.0.0",
        "windows": ">= 0.0.0"
    },
    "platforms": {
        "amazon": ">= 0.0.0",
        "arch": ">= 0.0.0",
        "centos": ">= 0.0.0",
        "debian": ">= 0.0.0"
    },
    "recommendations": {}
}

//我的数据4.json:

// My data 4.json:

{
    "dependencies": {
        "dmg": ">= 0.0.0",
        "build-essential": ">= 0.0.0",
        "windows": ">= 0.0.0"
    }
}

//我的数据5.json(压缩):

// My data 5.json (compress):

{"dependencies":{"dmg":">= 0.0.0","build-essential":">= 0.0.0","windows":">= 0.0.0"},"platforms":{"amazon":">= 0.0.0","arch":">= 0.0.0","centos":">= 0.0.0","debian":">= 0.0.0"},"recommendations":{}}

推荐答案

sed -n '/dependencies/, /}/ p' t|grep '>='

<小时>这是如何工作的:

首先获取dependencies块之间的文本,然后提取依赖.

First get the text between dependencies block, and then extract the dependencies.

请注意,此方法与依赖项块所在的文本中的位置无关.只要它存在,你就会得到答案.

Note that this method is independent of where in the text the dependency block is located. As long as it is present, you'll get the answer.

aman@apollo:~$ sed -n '/dependencies/, /}/ p' t|grep '>='
        "dmg": ">= 0.0.0",
        "build-essential": ">= 0.0.0",
        "windows": ">= 0.0.0"

<小时>

使用 sed -n '/dependencies/,/}/p' t|grep '.*='

如果依赖块中可以有像 ~== 这样的符号(而不仅仅是 >=).

If there can be symbols like ~=, = in the dependency block (and not just >=).

<小时>压缩版对于压缩版本,您可以先解压缩"(插入换行符)文件,然后应用相同的转换.


Compressed version For the compressed version, you can first "decompress" (insert newlines) the file and then apply the same transformation.

sed -e 's/:{/:{
/g'  -e  's/},/
},
/g' d5|sed -n '/dependencies/, /}/ p'|grep '>='

原始解决方案适用于所有其他 4 个文件.

The original solution will work for all the 4 other files.

这篇关于Bash Shell 脚本提取 Json 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆