I have a file which contains below details and it's repeats multiple times. I want to extract the Countryid and History value from below text file
The output should be like
Countryid: 0115 History: 20220621
Could you please help how can I extract above string from this text file using Unix script.
{
"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"
]
},
CodePudding user response:
If this is valid json, one solution is to use a proper json parsing tool, such as jq, e.g.
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
CodePudding user response:
Assumptions:
- OP wants to print all
Countryid/History
pairs - the
Countryid
entry may come before or afterHistory
entry - each
Countryid
entry must have a matchingHistory
entry - all sets have a
Music
entry that comes before theCountryid/History
entries
One awk
idea:
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
If we can assume that Countryid
always comes before History
then we can reduce the code to:
awk -F '"' '
$2 == "Countryid" { countryid=$4 }
$2 == "History" { printf "Countryid: %s History: %s\n", countryid, h$4 }
' input.dat
Both of these generate:
Countryid: 0115 History: 20220621
Countryid: 1312 History: 20221012