简体   繁体   中英

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

sed 's not a great tool for that task because it's a line-based text edition tool.

You had to use N to consume an additional line so that your working buffer would contain the linefeed separating both lines ; 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 .

One possible solution with sed would be to consume the whole file in memory before doing the replacement, as shown in this answer :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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