unix 如何从下面的模式中提取字符串

yebdmbv4  于 2023-02-04  发布在  Unix
关注(0)|答案(2)|浏览(132)

我有一个包含以下详细信息的文件,它重复多次。我想从下面的文本文件中提取CountryID和历史值
输出应如下所示

Countryid: 0115 History: 20220621

你能帮助我如何使用Unix脚本从这个文本文件中提取上述字符串吗?

{
            "Music": "1410",
            "Countryid": "0115",
            "History": "20220621",
            "Legend": "/api/legacysbo/bondue",
            "Sorting": "/api/dmplus/test",
            "Nick": "hinduja",
            "Scenario": [
                "K",
                "A",
                "S",
                "F",
                "D"
            ]
        },
        {
            "Music": "1466",
            "Countryid": "1312",
            "History": "20221012",
            "Legend": "/api/legacysbo/grenob",
            "Sorting": "/api/dmplus/prod",
            "Nick": "Grenoble",
            "Scenario": [
                "K",
                "A",
                "S",
                "F",
                "D"
            ]
        },
gr8qqesn

gr8qqesn1#

如果这是有效的json,一个解决方案是使用适当的json解析工具,例如jq

cat test.json
[{
    "Music": "1410",
    "Countryid": "0115",
    "History": "20220621",
    "Legend": "/api/legacysbo/bondue",
    "Sorting": "/api/dmplus/test",
    "Nick": "hinduja",
    "Scenario": ["K", "A", "S", "F", "D"]
}, {
    "Music": "1466",
    "Countryid": "1312",
    "History": "20221012",
    "Legend": "/api/legacysbo/grenob",
    "Sorting": "/api/dmplus/prod",
    "Nick": "Grenoble",
    "Scenario": ["K", "A", "S", "F", "D"]
}]

jq -r '.[] | "Countryid: \(.Countryid) History: \(.History)"' < test.json
Countryid: 0115 History: 20220621
Countryid: 1312 History: 20221012
n9vozmp4

n9vozmp42#

假设:

  • OP想要打印 * 所有 * Countryid/History
  • Countryid条目可以在History条目之前或之后出现
  • 每个Countryid条目必须具有匹配的History条目
  • 所有集都有一个Music条目,该条目位于Countryid/History条目之前

一个awk创意:

awk -F '"' '

function print_pair() {
    if (countryid && history)                                      # if both variables are non-empty then ...
       printf "Countryid: %s History: %s\n", countryid, history

    countryid=histor=""                                            # reset variables
}

$2 == "Music"     { print_pair() }                                 # print previous pair to stdout
$2 == "Countryid" { countryid=$4 }
$2 == "History"   { history=$4   }
END               { print_pair() }                                 # print last pair to stdout
' input.dat

如果我们可以假设Countryid总是在History之前,那么我们可以将代码简化为:

awk -F '"' '
$2 == "Countryid" { countryid=$4 }
$2 == "History"   { printf "Countryid: %s History: %s\n", countryid, h$4   }
' input.dat

这两种方法都会产生:

Countryid: 0115 History: 20220621
Countryid: 1312 History: 20221012

相关问题