Issue
Is it feasible to remove a block of lines (including starting {
and ending },
) only if "is_staff": true
?
The text file content would be always in this format:
[
{
"model": "accounts.account",
"pk": 2,
"fields": {
"password": "pbkdf2_sha256$320000$5vD4W5YDuvMmyyVRN5ub6U$weJzPTlN1Hrd7dlpjeO2Lw29a8ej+60Ib09vEifi4Qk=",
"last_login": "2022-09-08T14:20:45.106Z",
"is_superuser": false,
"licence": "dde0af69-154a-4b8c-b393-bd59730b5947",
"email": "[email protected]",
"first_name": "Marco",
"last_name": "Rossi",
"piva": "02375050511",
"is_active": true,
"is_staff": false,
"date_joined": "2022-09-07T14:10:22.456Z",
"groups": [
1
],
"user_permissions": []
}
},
{
"model": "accounts.account",
"pk": 1,
"fields": {
"password": "pbkdf2_sha256$320000$mh3UDxtwhiROEecrz8XH3o$NmqWo9epps2EQnYYTe8bKm72HqfHImxUE/eVSZWQ/1U=",
"last_login": "2022-09-09T13:54:09.498Z",
"is_superuser": true,
"licence": null,
"email": "[email protected]",
"first_name": "",
"last_name": "",
"piva": "",
"is_active": true,
"is_staff": true,
"date_joined": "2022-09-07T14:05:03.213Z",
"groups": [],
"user_permissions": []
}
},
{
"model": "accounts.account",
"pk": 3,
"fields": {
"password": "pbkdf2_sha256$320000$n4PRBAgTo8AejcTf66LTEr$SxhmWFvIcf+gWDyzNXpWz0MrfjemaX1a2msBxUZNvBc=",
"last_login": null,
"is_superuser": false,
"licence": "dde0af69-154a-4b8c-b393-bd59730b5947",
"email": "ac[email protected]",
"first_name": "Alessandro",
"last_name": "Bianchi",
"piva": "",
"is_active": true,
"is_staff": false,
"date_joined": "2022-09-07T14:11:39.207Z",
"groups": [
3
],
"user_permissions": []
}
},
{
...
}
]
Preferred result:
[
{
"model": "accounts.account",
"pk": 2,
"fields": {
"password": "pbkdf2_sha256$320000$5vD4W5YDuvMmyyVRN5ub6U$weJzPTlN1Hrd7dlpjeO2Lw29a8ej+60Ib09vEifi4Qk=",
"last_login": "2022-09-08T14:20:45.106Z",
"is_superuser": false,
"licence": "dde0af69-154a-4b8c-b393-bd59730b5947",
"email": "[email protected]",
"first_name": "Marco",
"last_name": "Rossi",
"piva": "02375050511",
"is_active": true,
"is_staff": false,
"date_joined": "2022-09-07T14:10:22.456Z",
"groups": [
1
],
"user_permissions": []
}
},
{
"model": "accounts.account",
"pk": 3,
"fields": {
"password": "pbkdf2_sha256$320000$n4PRBAgTo8AejcTf66LTEr$SxhmWFvIcf+gWDyzNXpWz0MrfjemaX1a2msBxUZNvBc=",
"last_login": null,
"is_superuser": false,
"licence": "dde0af69-154a-4b8c-b393-bd59730b5947",
"email": "[email protected]",
"first_name": "Alessandro",
"last_name": "Bianchi",
"piva": "",
"is_active": true,
"is_staff": false,
"date_joined": "2022-09-07T14:11:39.207Z",
"groups": [
3
],
"user_permissions": []
}
},
{
...
}
]
Solution
As you have now changed the format of your input to actual JSON, it is much simpler and more reliable to use a json-aware tool such as jq
:
jq '[ .[] | select(.fields.is_staff!=true) ]' <infile >outfile
This will pretty-print, so output will be indented differently from the input sample. However, it does not change the meaning of the JSON.
Answered By - jhnc Answer Checked By - Katrina (WPSolving Volunteer)