如何在 bash 中选择特定行及其特定列?

如何在 bash 中选择特定行及其特定列?

例如我有这个数据,我想得到输出

serNo:  ASDFAFDAFD3223
                         profileType": "Asset Tag: profileValue": "12341233
corrected ouput as serNo:  ASDFAFDAFD3223
                Asset Tag: 12341233
_______________________________________________________________________
{
                "assetTag": null,
                "category": "Switch",
                "children": [
                    {
                        "profileType": "Asset Tag",
                        "profileValue": "12341233"
                    }
                ],
                "customerIpn": "234-234324-234",
                "loc": "23",
                "mfgPartNo": "ADSF-32C-ASDF",
                "parentSn": "APLADFKJAOFHKNFDSOIH",
                "partNo": "JPR-ADF-32C-ASDF",
                "position": "23",
                "serNo": "ASDFAFDAFD3223",
                "skuNo": 4125056,
                "subLoc": "1",
                "usageType": null,
                "vendor": "ASDFADSF NETWORKS (U.S.) INC"
            } 

我尝试过类似的方法

 grep -e Switch -A4 | awk -F":" '/"profileValue"/{print $2}'
 "12341233"
 "ASDFKHKSHDF980"
 "ASDFDSF8689087"
 "SADFASIYYI86"
 "KHDSFKAYUSF86"
 "87698234"
 "97823407"

但无法获得 Serno 和 Asset Tag 等预期数据

答案1

处理 JSON 文件的最佳方法是使用jq.

然后通过管道传输文件jq '.serNo'会给你:

"ASDFAFDAFD3223"

jq '.children[].profileValue'给你:

"12341233"

您有-r删除".sed如果您想添加前缀,可以轻松处理这两个命令的输出。

相关内容