简体   繁体   English

sed不能替代所有匹配项

[英]sed does not replace all the matches

I have this on a file: 我在文件上有这个:

Structural Attributes:    text
                          text_id              [A]
                          text_lleng_tr        [A]
                          text_lleng_or        [A]
                          text_cpr             [A]
                          text_for             [A]
                          text_ftr             [A]
                          text_indexador       [A]
                          text_dif             [A]
                          text_reg             [A]
                          text_esp             [A]
                          text_tem             [A]
                          text_tipus           [A]
                          text_data_or         [A]
                          text_data_tr         [A]
                          text_autor           [A]
                          text_traductor       [A]
                          text_titol_or        [A]
                          text_titol_tr        [A]
                          s
                          s_id                 [A]
                          enty
                          contrac
                          contrac_forma        [A]
                          abr
                          date
                          p

Then I run this sed command on it: 然后我在上面运行此sed命令:

sed -i "N;s/\[A\]\n/,/g" file

From which I get: 从中我得到:

Structural Attributes:    text
                          text_id              ,                          text_lleng_tr        [A]
                          text_lleng_or        ,                          text_cpr             [A]
                          text_for             ,                          text_ftr             [A]
                          text_indexador       ,                          text_dif             [A]
                          text_reg             ,                          text_esp             [A]
                          text_tem             ,                          text_tipus           [A]
                          text_data_or         ,                          text_data_tr         [A]
                          text_autor           ,                          text_traductor       [A]
                          text_titol_or        ,                          text_titol_tr        [A]
                          s
                          s_id                 [A]
                          enty
                          contrac
                          contrac_forma        ,                          abr
                          date
                          p

So, as you can see there still are [A]+line jump left, if I run the sed command a second time on the same file the amount of [A]+line jump decreases, but I have to run the command 3 times for the [A]+line jump to disappear. 因此,您可以看到仍然有[A] +行跳转,如果我在同一文件上第二次运行sed命令,则[A] +行跳转的数量会减少,但是我必须运行命令3次使[A] +线跳转消失。 So the question is, am I doing something wrong or what is the correct way to do it for the [A]+line jump to be replaced at once. 所以问题是,我是在做错什么,还是立即替换[A] +跳线的正确方法是什么?

sed 's not a great tool for that task because it's a line-based text edition tool. sed并不是完成该任务的好工具,因为它是基于行的文本编辑工具。

You had to use N to consume an additional line so that your working buffer would contain the linefeed separating both lines ; 您必须使用N来占用额外的一行,以便您的工作缓冲区中将包含分隔两行的换行符; the problem is that this only gives you the ability to work on half the linefeeds each time because once the line is consumed by N it won't be consumed by the next pass of sed : your first pass replaces the linefeed between line1\\nline2 , then the second pass the one between line3\\nline4 . 问题在于,这仅使您每次都能处理一半换行符,因为一旦行被N占用,它就不会被sed的下一遍占用:您的第一遍将替换line1\\nline2之间的换行符,然后第二遍在line3\\nline4之间line3\\nline4

One possible solution with sed would be to consume the whole file in memory before doing the replacement, as shown in this answer : sed一种可能解决方案是在进行替换之前先将整个文件消耗在内存中,如以下答案所示:

sed -i ':a;N;$!ba;s/\[A\]\n/, /g' file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM