如何通过BASH中的sed命令从Json文件中删除Json对象 [英] How to delete a Json object from Json File by sed command in BASH

查看:298
本文介绍了如何通过BASH中的sed命令从Json文件中删除Json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要删除json对象的json文件. 有什么方法只能通过sed命令将其删除. 我的示例文件如下.

I am having an json file in which a json object need to be deleted. is there any way to delete it by sed command only. my sample file is given below.

{
        "name": "ABC",
        "description": "XYZ",
        "versions": {

        },
        "json_class": "Chef::Environment",
        "chef_type": "environment",
        "default_attributes": {

        },
        "override_attributes": {
          "company": {
            "xyz": {
                    "mailer":{
        "smtp_host": "Some IP",
        "imap_host": "Some Host",
    "imap_user": "Some UserId",
    "reply_to": "Some User Id",
    "outbound_user": "",
    "imap_mail_domain": "compute.oraclecloud.com",
    "imap_password": ""},

                    "logicaldomainname": ""
                      }
                      }
        }
    }

在此文件中,我想通过sed命令从文件中删除邮件对象. 请看一下,让我知道sed命令是否可行.

In this file i want to delete mailer object from the file by sed command. Please take a look and let me know if it is possible by sed command.

此删除操作的主要目的是用新的替换该对象 邮件对象. 这样,下次发送另一个邮件对象时,我们将其附加到xyz json对象中.

my main motive of this deletion is to replace this object with new mailer object. so that next time when another mailer object will be posted than we will append it in xyz json object.

预期输出:

{
        "name": "ABC",
        "description": "",
        "version": {

        },
        "json_class": "Chef::Environment",
        "chef_type": "environment",
        "default_attributes": {

        },
        "override_attributes": {
          "Company": {
            "XYZ": {
                    "logicaldomainname": ""
                      }
                      }
        }
    }

推荐答案

使用sed:

sed '/\"mailer\"/,/}/ d; /^$/d' 1.a

mailer删除到第一个},然后删除所有空白行(您输入的行有一个)".

"Delete from mailer to the first }, then delete all blank lines (you input has one)".

覆盖文件(就地")

sed -i ...

注意:适用于多行文件(与您一样).

这篇关于如何通过BASH中的sed命令从Json文件中删除Json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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