1

我有以下格式的 json 文件:

[
    {
        "organization": "ABC", 
        "type": "School", 
        "contact": "Joe Schmo", 
        "contact_title": "Principal", 
        "mailing_address": "123 Main Street, Anytown, MA", 
        "phone": "214-555-5430",
        "fax": "214-555-5444"
    },
    {
        "organization": "XYZ",
        "type": "School",
        "contact": "John Doe", 
        "contact_title": "Asst Principal", 
        "mailing_address": "123 Main Street, Anycity, TX", 
        "phone": "512-555-5430",
        "fax": "512-555-5444"
    },
    .
    .
    .
    .
]

我想复制以开头的行,然后在替换为and"organization"后将其添加回文件两次。我也想保留原线。我想要的输出是:"organization""company""long name"

[
    {
        "organization": "ABC",
        "company": "ABC",
        "long name": "ABC", 
        "type": "School", 
        "contact": "Joe Schmo", 
        "contact_title": "Principal", 
        "mailing_address": "123 Main Street, Anytown, MA", 
        "phone": "214-555-5430",
        "fax": "214-555-5444"
    },
    {
        "organization": "XYZ",
        "company": "XYZ",
        "name": "XYZ",
        "type": "School",
        "contact": "John Doe", 
        "contact_title": "Asst Principal", 
        "mailing_address": "123 Main Street, Anycity, TX", 
        "phone": "512-555-5430",
        "fax": "512-555-5444"
    },
    .
    .
    .
    .
]

awksed首选解决方案。

4

1 回答 1

4

这是一种方法:

sed '/organization/p;s/organization/company/p;s/company/long name/' file

这是另一个:

awk '$1~/organization/{print $0;sub(/organization/,"company");print $0;sub(/company/,"long name")}1' file
于 2013-03-21T20:00:31.827 回答