'How to delete the whole content within { } if a keyword is found?

So, I have a file with various entries contained in { } parenthesis. Example:

{
"classname" "waypoint"
"targetname" "w1"
"target" "w2"
"origin" "672 -1432 32"
"target2" "w9"
}
{
"classname" "light"
"light" "500"
"scale" "4"
"origin" "672 -1440 232"
}
{
"classname" "NPC_Tavion"
"angle" "180"
"origin" "860 -1092 -8"
}
{
"classname" "info_player_start"
"angle" "360"
"origin" "312 -1080 216"
}
{
"classname" "light"
"light" "500"
"scale" "4"
"origin" "320 -1304 232"
}

I want to delete the whole content within { } alongside the { } itself if a word NPC_ is found. So the outcome I want is:

{
"classname" "waypoint"
"targetname" "w1"
"target" "w2"
"origin" "672 -1432 32"
"target2" "w9"
}
{
"classname" "light"
"light" "500"
"scale" "4"
"origin" "672 -1440 232"
}
{
"classname" "info_player_start"
"angle" "360"
"origin" "312 -1080 216"
}
{
"classname" "light"
"light" "500"
"scale" "4"
"origin" "320 -1304 232"
}

I've found a thing that could do it within AWK, but I can only use Windows tools and/or Python. I have come up with a thing like that (well, I just found another codepiece and modified it):

bad_words = ['NPC_']

with open('oldfile.txt') as oldfile, open('newfile.txt', 'w') as newfile:
    for line in oldfile:
        if not any(bad_word in line for bad_word in bad_words):
            newfile.write(line)

However, I have no idea how to make it include the other content within { }. I have found this question: Remove text between () and [] and I figured out something like that could work:

    ret = ''
    skip1c = 0
    skip2c = 0
    for i in test_str:
        if i == '{':
            skip1c += 1
        elif i == '}' and skip1c > 0:
            skip1c -= 1
        elif skip1c == 0 and skip2c == 0:
            ret += i
    return ret```

But I have no idea how to mix the two :(


Solution 1:[1]

Let s be your string containing the content of the file.

out = ''.join('{' + g for g in s.split('{') if 'NPC_' not in g and len(g) > 1)

A one-liner that doesn't use regex. It divides your string into groups g by splitting the string at {. If a group contains NPC_ or is an empty split, it's ignored for the output. ''.join(..) stitches the string back together.

This Python structure is called generator expression and acts as a for-loop that builds lists or other iterables.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1