Find a block of text and if a certain keyword exists, remove another block of text within that block

Platform: CentOS Linux
Language: Shell

Hello. So I have a text file named goob, containing the following contents:

		function-a()=""
                {
			type=scythe
			value-b=true
			{
				under="false"{}
			}
		}
		function-b()=""
                {
			type=hook
			value-b=true
			{
				under="false"{}
			}
		}
		function-c()=""
                {
			type=scythe
			value-b=true
			{
				under="false"{}
			}
		}

What needs to happen is for all functions with type "scythe", I need to remove the whole block of their value-b such that the result would be

		function-a()=""
                {
			type=scythe
		}
		function-b()=""
                {
			type=hook
			value-b=true
			{
				under="false"{}
			}
		}
		function-c()=""
                {
			type=scythe
		}

Any ideas? Thanks in advance.

Hi pulpwilkes and welcome to the forums

Some points missing:

  • Use code tags for code
  • What OS
  • What language / shell?

--- Post updated at 06:35 ---

Thanks for the updates.

  1. Your 'desired' output still contains a value-b=true 'block' --> purpose or accident?
  2. What have you tried so far? (sry forgot to ask before)
2 Likes
  1. For function-b, the value-b=true block remains as its type is hook, not scythe.
  2. I've tried researching on sed but the closest I came to is its function of removing a whole data block from point A to point b i.e. all value-b=true blocks regardless of their type.

oops :o

As in, what code do you have so far?

Here is one with awk:

awk '
{ blevel+=gsub("[{]","&")-gsub("[}]","&") }
blevel<dlevel { del=0 }
del==0
$1~/^type=scythe/ { del=1; dlevel=blevel }
' goob

When it meets type=scythe it deletes the following lines until it leaves the current { block } .