如何使用linux解析json与shell脚本

Meg*_*rma 8 linux shell awk json sed

我有一个Json输出,我需要在linux中提取一些参数.

这是json输出:

{
  "OwnerId":"121456789127",
  "ReservationId":"r-48465168",
  "Groups":[

  ],
  "Instances":[
    {
      "Monitoring":{
        "State":"disabled"
      },
      "PublicDnsName":null,
      "RootDeviceType":"ebs",
      "State":{
        "Code":16,
        "Name":"running"
      },
      "EbsOptimized":false,
      "LaunchTime":"2014-03-19T09:16:56.000Z",
      "PrivateIpAddress":"10.250.171.248",
      "ProductCodes":[
        {
          "ProductCodeId":"aacglxeowvn5hy8sznltowyqe",
          "ProductCodeType":"marketplace"
        }
      ],
      "VpcId":"vpc-86bab0e4",
      "StateTransitionReason":null,
      "InstanceId":"i-1234576",
      "ImageId":"ami-b7f6c5de",
      "PrivateDnsName":"ip-10-120-134-248.ec2.internal",
      "KeyName":"Test_Virginia",
      "SecurityGroups":[
        {
          "GroupName":"Test",
          "GroupId":"sg-12345b"
        }
      ],
      "ClientToken":"VYeFw1395220615808",
      "SubnetId":"subnet-12345314",
      "InstanceType":"t1.micro",
      "NetworkInterfaces":[
        {
          "Status":"in-use",
          "SourceDestCheck":true,
          "VpcId":"vpc-123456e4",
          "Description":"Primary network interface",
          "NetworkInterfaceId":"eni-3619f31d",
          "PrivateIpAddresses":[
            {
              "Primary":true,
              "PrivateIpAddress":"10.120.134.248"
            }
          ],
          "Attachment":{
            "Status":"attached",
            "DeviceIndex":0,
            "DeleteOnTermination":true,
            "AttachmentId":"eni-attach-9210dee8",
            "AttachTime":"2014-03-19T09:16:56.000Z"
          },
          "Groups":[
            {
              "GroupName":"Test",
              "GroupId":"sg-123456cb"
            }
          ],
          "SubnetId":"subnet-31236514",
          "OwnerId":"109030037527",
          "PrivateIpAddress":"10.120.134.248"
        }
      ],
      "SourceDestCheck":true,
      "Placement":{
        "Tenancy":"default",
        "GroupName":null,
        "AvailabilityZone":"us-east-1c"
      },
      "Hypervisor":"xen",
      "BlockDeviceMappings":[
        {
          "DeviceName":"/dev/sda",
          "Ebs":{
            "Status":"attached",
            "DeleteOnTermination":false,
            "VolumeId":"vol-37ff097b",
            "AttachTime":"2014-03-19T09:17:00.000Z"
          }
        }
      ],
      "Architecture":"x86_64",
      "KernelId":"aki-88aa75e1",
      "RootDeviceName":"/dev/sda1",
      "VirtualizationType":"paravirtual",
      "Tags":[
        {
          "Value":"Server for testing RDS feature in us-east-1c AZ",
          "Key":"Description"
        },
        {
          "Value":"RDS_Machine (us-east-1c)",
          "Key":"Name"
        },
          {
          "Value":"1234",
          "Key":"Cost.centre"
        },
        {
          "Value":"Jyoti Bhanot",
          "Key":"Owner"
        }
      ],
      "AmiLaunchIndex":0
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

预期产量:

 Instance id         Name                           cost centre             Owner
    i-1234576          RDS_Machine (us-east-1c)        1234                   Jyoti Bhanot
Run Code Online (Sandbox Code Playgroud)

我想写一个文件,其中包含标题,如实例ID,标签,如名称,成本中心,所有者.在此之下,来自json输出的某些值.这里给出的输出只是一个例子.

我怎么能用sed和awk做到这一点?

任何领导都表示赞赏.

谢谢

M. *_*sha 13

这是使用jsawk的示例.参考:使用Unix工具解析JSON

建立:

首先jsawkhttps://github.com/micha/jsawk下载:

$ curl -L http://github.com/micha/jsawk/raw/master/jsawk > jsawk
$ chmod 755 jsawk && mv jsawk ~/bin/
Run Code Online (Sandbox Code Playgroud)

您可能希望先安装,js-devel然后才能使用jsawk.我正在使用Fedora,所以我做的是:

$ sudo yum install js-devel
Run Code Online (Sandbox Code Playgroud)

考试:

我将您的JSON输出示例复制到文本文件中.叫它sample.json.这是一个从JSON输出示例中获取值的示例:

$ jsawk 'return this.Instances[0].Monitoring.State' < sample.json
disabled
$ jsawk 'return this.Instances[0].VpcId' < sample.json
vpc-86bab0e4
Run Code Online (Sandbox Code Playgroud)

对于来自URL的JSON数据,您可以使用curl http://someserver.com/data.json而不是cat:

$ curl http://someserver.com/data.json | jsawk 'return this.Instances[0].VpcId'
vpc-86bab0e4
Run Code Online (Sandbox Code Playgroud)

您可以在bash文件中使用这些命令来生成包含所需字符串/文本的新文件.您可以jsawk从我在此提供的GitHub链接中了解更多信息.

这是你在找什么?

  • 正如[Rahul R Dhobi](http://stackoverflow.com/users/3184706/rahul-r-dhobi)所说,你可能想看到这个:http://stackoverflow.com/questions/1955505/parsing-json- with-sed-and-awk针对您的具体问题.[jsawk](https://github.com/micha/jsawk)可能是你的答案. (2认同)