Jab
Jab

Reputation: 129

Script to delete lines from a file when the line starts with certain word

I have a dot file like below.

....
29 [label="OutRet", fillcolor="#90EE90", shape=box];
30 [label="In(alloced_return_alloca[bits 0 to ..])", fillcolor="#6495ED",
      shape=box];

subgraph cluster_Call4 { label="Call4 : f = (int *)alloca(sizeof(*f) * __lengthof_f);";
                           fillcolor="#B38B4D"; style="filled"; 17;16;
     };

edge [dir=back];
7 -> 6 [color="#000000", style="dotted"];
....

Whenever there is a subgraph attribute, I would like to remove that attribute. In some files there might be multiple sugraph attributes in which case I would like to remove all of them. I tried using sed '/subgraph/ d' inputfile > outputFile and sed -i 's/subgraph.*//' file outputFilebut it removes just the line which contains subgraph and gives a result like below:

30 [label="In(alloced_return_alloca[bits 0 to ..])", fillcolor="#6495ED",
      shape=box];

                           fillcolor="#B38B4D"; style="filled"; 17;16;
     };

  edge [dir=back];

Is there any way where I could remove the other two lines also(In general how many ever associated lines) i.e., the lines starting from subgraph till the occurance of "edge". Also, is there a way where we can output our result in the same file instead of writing it in another file?

Thanks

Upvotes: 0

Views: 135

Answers (2)

randomir
randomir

Reputation: 18697

To delete a sequence of lines delimited by some patterns, you should use an address range (two addresses separated by a comma ,) with regexp addresses to select the lines between (including the first and the last), followed by a delete command:

sed '/subgraph/,/edge/d' -i file

Use the -i/--in-place flag to edit the file in place (with an optional backup).

Btw, identical result is obtained by inverting the logic (with !) and printing out everything, except the lines in that range:

sed -n '/subgraph/,/edge/!p' -i file

Note we use -n to suppress automatic printing of pattern space, instead printing it explicitly with p command for each line not matched.

Upvotes: 1

Jab
Jab

Reputation: 129

I found out sed '/subgraph/,/edge/d' inputFile this does the work.

Upvotes: 1

Related Questions