Я пытаюсь сделать это с помощью jq
json входного файла (сокращено):
{
"CIS": 698435,
"split": 3
}
{
"CIS": 698435,
"split": 1.5
}
{
"CIS": 918839,
"split": 4.5
}
{
"CIS": 918839,
"split": 2
}
Я хотел бы иметь для своего сценария оболочки:
{
"CIS": 698435,
"split": 4.5
}
{
"CIS": 918839,
"split": 6.5
}
Как я могу сделать, чтобы сделать сумму всех разделенных значений для определенного c ключа CIS?
Я попытался group_by с неудачно
cat input.json | jq -r '.'
пс : перед этим я делаю замену
cat /tmp/test.json |
jq -r ' if (.order[].orderInfo[].scannedCIS) != (0) then
( "replace " + (.order[].orderInfo[].CIS|tostring) + " with " +
(.order[].orderInfo[].scannedCIS|tostring)) else null end '
jq --version: jq-1.5-1-a5b5cbeode
Реальный файл выглядит так:
cat /tmp/export/export_200519110544.json | jq -rs '.[].order[].orderInfo[] | { CIS:.CIS , split:.split } '
{
"CIS": 653397,
"split": 1
}
{
"CIS": 653397,
"split": 1
}
{
"CIS": 653397,
"split": 1
}
{
"CIS": 653397,
"split": 1
}
{
"CIS": 653397,
"split": 1
}
{
"CIS": 653397,
"split": 1
}
{
"CIS": 653397,
"split": 1
}
{
"CIS": 698435,
"split": 1
}
{
"CIS": 698435,
"split": 1
}
{
"CIS": 698435,
"split": 1
}
{
"CIS": 698435,
"split": 1
}
{
"CIS": 698435,
"split": 1
}
{
"CIS": 698435,
"split": 1
}
{
"CIS": 698435,
"split": 1
}
{
"CIS": 918839,
"split": 1
}
{
"CIS": 918839,
"split": 1
}
{
"CIS": 918839,
"split": 1
}
{
"CIS": 918839,
"split": 1
}
{
"CIS": 918839,
"split": 1
}
{
"CIS": 918839,
"split": 1
}
{
"CIS": 918839,
"split": 1
}
{
"CIS": 603564,
"split": 1
}
{
"CIS": 603564,
"split": 1
}
{
"CIS": 603564,
"split": 1
}
{
"CIS": 603564,
"split": 1
}
{
"CIS": 603564,
"split": 1
}
{
"CIS": 603564,
"split": 1
}
{
"CIS": 603564,
"split": 1
}
{
"CIS": 603564,
"split": 1
}
{
"CIS": 603564,
"split": 1
}
{
"CIS": 603564,
"split": 1
}
{
"CIS": 603564,
"split": 1
}
{
"CIS": 603564,
"split": 1
}
{
"CIS": 603564,
"split": 1
}
{
"CIS": 603564,
"split": 1
}
формат ввода кажется идентичным, но я получаю сообщение об ошибке.
cat /tmp/export/export_200519110544.json | jq -rs '.[].order[].orderInfo[] | { CIS:.CIS , split:.split } | group_by(.CIS)[] | {CIS: .[0].CIS, split: [.[].split] | add} '
jq: error (at <stdin>:0): Cannot index number with string "CIS"
cat /tmp/export/export_200519110544.json
{"order":[{"customerId":0,"medicalStructureId":0,"calendarId":0,"calendarStartDate":"20200224","userLogin":"jlemoule","endOfRefillTS":1582106743807,"refillStatus":"FULL","orderInfo":[{"boxDay":0,"boxPeriod":1,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":653397,"scannedCIS":0,"scannedCISLabel":"AMLODIPINO CINFA 5 mg COMPRIMIDOS EFG","fromReimportCIS":0,"CIP":"8470006533970","lot":"BN4171","expDay":1,"expMonth":9,"expYear":21},{"boxDay":1,"boxPeriod":1,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":653397,"scannedCIS":0,"scannedCISLabel":"AMLODIPINO CINFA 5 mg COMPRIMIDOS EFG","fromReimportCIS":0,"CIP":"8470006533970","lot":"BN4171","expDay":1,"expMonth":9,"expYear":21},{"boxDay":2,"boxPeriod":1,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":653397,"scannedCIS":0,"scannedCISLabel":"AMLODIPINO CINFA 5 mg COMPRIMIDOS EFG","fromReimportCIS":0,"CIP":"8470006533970","lot":"BN4171","expDay":1,"expMonth":9,"expYear":21},{"boxDay":3,"boxPeriod":1,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":653397,"scannedCIS":0,"scannedCISLabel":"AMLODIPINO CINFA 5 mg COMPRIMIDOS EFG","fromReimportCIS":0,"CIP":"8470006533970","lot":"BN4171","expDay":1,"expMonth":9,"expYear":21},{"boxDay":4,"boxPeriod":1,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":653397,"scannedCIS":0,"scannedCISLabel":"AMLODIPINO CINFA 5 mg COMPRIMIDOS EFG","fromReimportCIS":0,"CIP":"8470006533970","lot":"BN4171","expDay":1,"expMonth":9,"expYear":21},{"boxDay":5,"boxPeriod":1,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":653397,"scannedCIS":0,"scannedCISLabel":"AMLODIPINO CINFA 5 mg COMPRIMIDOS EFG","fromReimportCIS":0,"CIP":"8470006533970","lot":"BN4171","expDay":1,"expMonth":9,"expYear":21},{"boxDay":6,"boxPeriod":1,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":653397,"scannedCIS":0,"scannedCISLabel":"AMLODIPINO CINFA 5 mg COMPRIMIDOS EFG","fromReimportCIS":0,"CIP":"8470006533970","lot":"BN4171","expDay":1,"expMonth":9,"expYear":21},{"boxDay":0,"boxPeriod":2,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":false,"specialProtocol":false,"surplus":false,"CIS":698435,"scannedCIS":0,"scannedCISLabel":"PECTOX LISINA 2,7 g granulado","fromReimportCIS":0,"CIP":"698435","lot":"19019","expDay":30,"expMonth":11,"expYear":21},{"boxDay":1,"boxPeriod":2,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":false,"specialProtocol":false,"surplus":false,"CIS":698435,"scannedCIS":0,"scannedCISLabel":"PECTOX LISINA 2,7 g granulado","fromReimportCIS":0,"CIP":"698435","lot":"19019","expDay":30,"expMonth":11,"expYear":21},{"boxDay":2,"boxPeriod":2,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":false,"specialProtocol":false,"surplus":false,"CIS":698435,"scannedCIS":0,"scannedCISLabel":"PECTOX LISINA 2,7 g granulado","fromReimportCIS":0,"CIP":"698435","lot":"19019","expDay":30,"expMonth":11,"expYear":21},{"boxDay":3,"boxPeriod":2,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":false,"specialProtocol":false,"surplus":false,"CIS":698435,"scannedCIS":0,"scannedCISLabel":"PECTOX LISINA 2,7 g granulado","fromReimportCIS":0,"CIP":"698435","lot":"19019","expDay":30,"expMonth":11,"expYear":21},{"boxDay":4,"boxPeriod":2,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":false,"specialProtocol":false,"surplus":false,"CIS":698435,"scannedCIS":0,"scannedCISLabel":"PECTOX LISINA 2,7 g granulado","fromReimportCIS":0,"CIP":"698435","lot":"19019","expDay":30,"expMonth":11,"expYear":21},{"boxDay":5,"boxPeriod":2,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":false,"specialProtocol":false,"surplus":false,"CIS":698435,"scannedCIS":0,"scannedCISLabel":"PECTOX LISINA 2,7 g granulado","fromReimportCIS":0,"CIP":"698435","lot":"19019","expDay":30,"expMonth":11,"expYear":21},{"boxDay":6,"boxPeriod":2,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":false,"specialProtocol":false,"surplus":false,"CIS":698435,"scannedCIS":0,"scannedCISLabel":"PECTOX LISINA 2,7 g granulado","fromReimportCIS":0,"CIP":"698435","lot":"19019","expDay":30,"expMonth":11,"expYear":21},{"boxDay":0,"boxPeriod":1,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":918839,"scannedCIS":0,"scannedCISLabel":"KEPPRA 500 mg COMPRIMIDOS RECUBIERTOS CON PELICULA","fromReimportCIS":0,"CIP":"918839","lot":"273460","expDay":31,"expMonth":8,"expYear":21},{"boxDay":1,"boxPeriod":1,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":918839,"scannedCIS":0,"scannedCISLabel":"KEPPRA 500 mg COMPRIMIDOS RECUBIERTOS CON PELICULA","fromReimportCIS":0,"CIP":"918839","lot":"273460","expDay":31,"expMonth":8,"expYear":21},{"boxDay":2,"boxPeriod":1,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":918839,"scannedCIS":0,"scannedCISLabel":"KEPPRA 500 mg COMPRIMIDOS RECUBIERTOS CON PELICULA","fromReimportCIS":0,"CIP":"918839","lot":"273460","expDay":31,"expMonth":8,"expYear":21},{"boxDay":3,"boxPeriod":1,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":918839,"scannedCIS":0,"scannedCISLabel":"KEPPRA 500 mg COMPRIMIDOS RECUBIERTOS CON PELICULA","fromReimportCIS":0,"CIP":"918839","lot":"273460","expDay":31,"expMonth":8,"expYear":21},{"boxDay":4,"boxPeriod":1,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":918839,"scannedCIS":0,"scannedCISLabel":"KEPPRA 500 mg COMPRIMIDOS RECUBIERTOS CON PELICULA","fromReimportCIS":0,"CIP":"918839","lot":"273460","expDay":31,"expMonth":8,"expYear":21},{"boxDay":5,"boxPeriod":1,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":918839,"scannedCIS":0,"scannedCISLabel":"KEPPRA 500 mg COMPRIMIDOS RECUBIERTOS CON PELICULA","fromReimportCIS":0,"CIP":"918839","lot":"273460","expDay":31,"expMonth":8,"expYear":21},{"boxDay":6,"boxPeriod":1,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":918839,"scannedCIS":0,"scannedCISLabel":"KEPPRA 500 mg COMPRIMIDOS RECUBIERTOS CON PELICULA","fromReimportCIS":0,"CIP":"918839","lot":"273460","expDay":31,"expMonth":8,"expYear":21},{"boxDay":0,"boxPeriod":1,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":603564,"scannedCIS":0,"scannedCISLabel":"BROMAZEPAM NORMON 1,5 mg CAPSULAS DURAS EFG","fromReimportCIS":0,"CIP":"8470006035641","lot":"321","expDay":0,"expMonth":2,"expYear":23},{"boxDay":1,"boxPeriod":1,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":603564,"scannedCIS":0,"scannedCISLabel":"BROMAZEPAM NORMON 1,5 mg CAPSULAS DURAS EFG","fromReimportCIS":0,"CIP":"8470006035641","lot":"321","expDay":0,"expMonth":2,"expYear":23},{"boxDay":2,"boxPeriod":1,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":603564,"scannedCIS":0,"scannedCISLabel":"BROMAZEPAM NORMON 1,5 mg CAPSULAS DURAS EFG","fromReimportCIS":0,"CIP":"8470006035641","lot":"321","expDay":0,"expMonth":2,"expYear":23},{"boxDay":3,"boxPeriod":1,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":603564,"scannedCIS":0,"scannedCISLabel":"BROMAZEPAM NORMON 1,5 mg CAPSULAS DURAS EFG","fromReimportCIS":0,"CIP":"8470006035641","lot":"321","expDay":0,"expMonth":2,"expYear":23},{"boxDay":4,"boxPeriod":1,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":603564,"scannedCIS":0,"scannedCISLabel":"BROMAZEPAM NORMON 1,5 mg CAPSULAS DURAS EFG","fromReimportCIS":0,"CIP":"8470006035641","lot":"321","expDay":0,"expMonth":2,"expYear":23},{"boxDay":5,"boxPeriod":1,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":603564,"scannedCIS":0,"scannedCISLabel":"BROMAZEPAM NORMON 1,5 mg CAPSULAS DURAS EFG","fromReimportCIS":0,"CIP":"8470006035641","lot":"321","expDay":0,"expMonth":2,"expYear":23},{"boxDay":6,"boxPeriod":1,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":603564,"scannedCIS":0,"scannedCISLabel":"BROMAZEPAM NORMON 1,5 mg CAPSULAS DURAS EFG","fromReimportCIS":0,"CIP":"8470006035641","lot":"321","expDay":0,"expMonth":2,"expYear":23},{"boxDay":0,"boxPeriod":2,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":603564,"scannedCIS":0,"scannedCISLabel":"BROMAZEPAM NORMON 1,5 mg CAPSULAS DURAS EFG","fromReimportCIS":0,"CIP":"8470006035641","lot":"321","expDay":0,"expMonth":2,"expYear":23},{"boxDay":1,"boxPeriod":2,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":603564,"scannedCIS":0,"scannedCISLabel":"BROMAZEPAM NORMON 1,5 mg CAPSULAS DURAS EFG","fromReimportCIS":0,"CIP":"8470006035641","lot":"321","expDay":0,"expMonth":2,"expYear":23},{"boxDay":2,"boxPeriod":2,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":603564,"scannedCIS":0,"scannedCISLabel":"BROMAZEPAM NORMON 1,5 mg CAPSULAS DURAS EFG","fromReimportCIS":0,"CIP":"8470006035641","lot":"321","expDay":0,"expMonth":2,"expYear":23},{"boxDay":3,"boxPeriod":2,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":603564,"scannedCIS":0,"scannedCISLabel":"BROMAZEPAM NORMON 1,5 mg CAPSULAS DURAS EFG","fromReimportCIS":0,"CIP":"8470006035641","lot":"321","expDay":0,"expMonth":2,"expYear":23},{"boxDay":4,"boxPeriod":2,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":603564,"scannedCIS":0,"scannedCISLabel":"BROMAZEPAM NORMON 1,5 mg CAPSULAS DURAS EFG","fromReimportCIS":0,"CIP":"8470006035641","lot":"321","expDay":0,"expMonth":2,"expYear":23},{"boxDay":5,"boxPeriod":2,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":603564,"scannedCIS":0,"scannedCISLabel":"BROMAZEPAM NORMON 1,5 mg CAPSULAS DURAS EFG","fromReimportCIS":0,"CIP":"8470006035641","lot":"321","expDay":0,"expMonth":2,"expYear":23},{"boxDay":6,"boxPeriod":2,"boxSubDivision":1,"split":1.0,"ifNecessary":false,"reconditionnable":true,"specialProtocol":false,"surplus":false,"CIS":603564,"scannedCIS":0,"scannedCISLabel":"BROMAZEPAM NORMON 1,5 mg CAPSULAS DURAS EFG","fromReimportCIS":0,"CIP":"8470006035641","lot":"321","expDay":0,"expMonth":2,"expYear":23}],"orderExtBoxInfo":[{"nbBoxes":1,"CIS":698435,"scannedCIS":698435,"scannedCISLabel":"PECTOX LISINA 2,7 g granulado","fromReimportCIS":0,"CIP":"698435","lot":"19019","expDay":30,"expMonth":11,"expYear":21}]}]}