Home > Software design >  SED: removing block of lines if condition is met?
SED: removing block of lines if condition is met?

Time:09-10

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": "[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": []
  }
},

CodePudding user response:

Sed solution: sed -n '/^{/ , /^,}/ { /^{/ h; //!H; /^},/ { g ;/is_staff": true/d; //!p } }' file

From { to }, accumulate in hold space.

On },:

  • g to get the hold space into pattern space
  • /is_staff": true/d to delete pattern space
  • //!p to print it otherwise

CodePudding user response:

sed '
    /^{$/ { # start of stanza - reset hold
        h
        d
    }

    # accumulate
    H

    /^},$/! d # next if not end of stanza

    # end of stanza - delete if match, else print
    x
    /"is_staff": true/ d
' file
  • Related