Question

I have a file whose contents are like this :

control_data {
some data dadadadf 
some data fsfsfs
some more data
.
.
.
}
more data below 
{
.
.
}

I want to delete the data from "control_data" to the first "}"

I tried a command

sed "s/control_data\([^}]*\)}//g"

but this only works if we do not have multiple lines eg. this command works when we have data like:

control_data {some data dadadadf some data fsfsfs some more data...}more data

It gives :

more data {....} 

How to make this command work when we have data across multiple lines ? I am new to shell scripting some explanation with answer will go a long way in helping.

Thanks in advance.

Was it helpful?

Solution

This one may not be an ideal one but this used sed(your sed code)

cat ip_file.txt | tr '\n' ':' | sed "s/control_data\([^}]*\)}//g" | tr ':' '\n'

Logic: I am converting all new lines to : and then using your sed code and then converting all : to newlines Note : My assumption is : wont be present in the file anywhere.

OTHER TIPS

My sed-fu is weak, but here's what I got working (using this answer as a guide):

[me@home]$ sed -n '/^control_data {/{:a;n;/^}/!ba;n};p' input_file.txt
more data below 
{
.
.
}

Here's a breakdown of the command

sed -n "             # "-n" = suppress automatic printing of pattern space
/^control_data {/ {  # if (line matches control data)
   :a;                 #   mark this spot with label a
   n;                  #   get next line
   /^}/!ba             #   if (doesn't start with "}") go back to label a
   n;                  #   get next line before leaving this control block
};                   # end if
p;                   # print everything else not affected by previous block
" input_file.txt

This might work for you:

sed '/^control_data {.*}$/d;/^control_data {/,/^}$/d' file
  • The first command removes all single lines /^control_data {.*}$/d
  • The second command removes all blocks /^control_data {/,/^}$/d
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top