当行以某个单词开头时,用于从文件中删除行的脚本

时间:2021-03-19 20:20:18

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:

每当有子图属性时,我想删除该属性。在某些文件中可能有多个sugraph属性,在这种情况下我想删除所有这些属性。我尝试使用sed'/ subgraph / d'inputfile> outputFile和sed -i's / subgraph。* //'文件outputFile但它只删除包含子图的行并给出如下结果:

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

谢谢

2 个解决方案

#1


1  

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

我发现sed'/ subgraph /,/ edge / d'inputFile这样做了。

#2


1  

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).

使用-i / - 就地标志来编辑文件(使用可选备份)。

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.

注意我们使用-n来禁止模式空间的自动打印,而是使用p命令为每个不匹配的行显式打印它。

#1


1  

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

我发现sed'/ subgraph /,/ edge / d'inputFile这样做了。

#2


1  

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).

使用-i / - 就地标志来编辑文件(使用可选备份)。

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.

注意我们使用-n来禁止模式空间的自动打印,而是使用p命令为每个不匹配的行显式打印它。