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

问题描述

在bash shell脚本,我想提取的对象。例如,下面的JSON文件,我想提取物依赖对象,它应该返回我:伤害:> = 0.0。 0,建立必要:> = 0.0.0,窗口:> = 0.0.0的任何格式,你如何做到这一点。

//我的数据1.json:

  {
    平台:{
        亚马逊:> = 0.0.0
        拱:> = 0.0.0
        centos的:&GT = 0.0.0,
        Debian的:> = 0.0.0
    },
    依赖:{
        伤害:&GT = 0.0.0,
        建立必要:> = 0.0.0
        窗口:&GT = 0.0.0
    },
    建议:{}
}

//我的数据2.json:

  {
    平台:{
        亚马逊:> = 0.0.0
        拱:> = 0.0.0
        centos的:&GT = 0.0.0,
        Debian的:> = 0.0.0
    },
    建议:{},
    依赖:{
        伤害:&GT = 0.0.0,
        建立必要:> = 0.0.0
        窗口:&GT = 0.0.0
    }
}

//我的数据3.json:

  {
    依赖:{
        伤害:&GT = 0.0.0,
        建立必要:> = 0.0.0
        窗口:&GT = 0.0.0
    },
    平台:{
        亚马逊:> = 0.0.0
        拱:> = 0.0.0
        centos的:&GT = 0.0.0,
        Debian的:> = 0.0.0
    },
    建议:{}
}

//我的数据4.json:

  {
    依赖:{
        伤害:&GT = 0.0.0,
        建立必要:> = 0.0.0
        窗口:&GT = 0.0.0
    }
}

//我的数据5.json(COM preSS):

  {依赖:{DMG:> = 0.0.0,集结必要:> = 0.0.0,窗口: > = 0.0.0},平台:{亚马逊:> = 0.0.0,牌坊:> = 0.0.0,CentOS的:> = 0.0 .0,Debian的:> = 0.0.0},建议:{}}


解决方案

  SED -n'/依赖/,/} / P'T | grep的'> ='



如何工作的:

首先获得块相关性之间的文本,然后提取的依赖关系。

注意,此方法是独立的其中,在文本的依赖块的位置。
只要是present,你会得到答案。


 祖阿曼@阿波罗:〜$ SED -n'/依赖/,/ \\} / P'T | grep的'> ='
        伤害:&GT = 0.0.0,
        建立必要:> = 0.0.0
        窗口:&GT = 0.0.0


使用 SED -n'/依赖/,/} / p'T | grep按'* ='

如果有可以在从属模块如〜= = 符号(而不仅仅是方式> =



的COM pressed版本
为COM pressed版本,你可以先DECOM preSS(插入新行)的文件,然后采用相同的转型。

 的sed -e'S / {/:{\\ N / G'-e'S /} / \\ n} \\ N / G'D5 | sed的-n /依赖/,/} / p'| grep的'> ='

最初的解决方案将所有的其他4文件。

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?

// 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": {}
}

// 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"
    }
}

// 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": {}
}

// My data 4.json:

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

// 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 '>='


How this works :

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"


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

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


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

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

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

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

08-28 22:10