【发布时间】:2014-05-05 21:15:59
【问题描述】:
我有一个 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
}
]
}
预期输出:
Instance id Name cost centre Owner
i-1234576 RDS_Machine (us-east-1c) 1234 Jyoti Bhanot
我想编写一个文件,其中包含诸如实例 ID 之类的标题、名称之类的标签、成本中心、所有者。在此之下,来自 json 输出的某些值。这里给出的输出只是一个例子。
如何使用 sed 和 awk 做到这一点?
感谢任何线索。
谢谢
【问题讨论】:
-
为什么要用
awk来解析JSON?您必须使用专用的 json 解析工具,例如jq或jsawk。我相信下面的答案应该对你有用。 -
对上面的示例没问题,你能提供你的预期输出吗?
-
你犯了同样的错误。您预期的输出与您提供的输入不匹配。输入json只有一个:
"InstanceId":"i-1234576" -
你忘了在
"Cost.center" }之后加上,