-   Linux - General (
-   -   sed search and replace question (

the_rhino 12-09-2012 10:26 PM

sed search and replace question
I want to search for a pattern across two consecutive lines and use part of that pattern to change part of the second line. I haven't been able to find anything about sed that would show me how to do it.

Everything I have found shows how to merge lines and add new blank lines but nothing shows how to do what I want.

I can do this all day long in Vim but I want to do it in the bash shell using sed or sed & awk or something else because I have several dozen files to do this on and opening them in Vim is painfully slow.

This single line below will find a pattern across two consecutive lines then put back everything one the first line and the second line with some added content.


s_^\([ </p>]*<h2 id="\)\(b0[0-9][0-9]c[0-1][0-9][0-9]\)\(" class="chapter">[0-9]\+</h
2><p>\)\n\([ ]*\)\([A-Z]\)_\1\2\3\r\4<span id="\2v001" class="verse">1</span> \5_

Does anyone know how to do this with sed or something else on the shell cli?

steelneck 12-09-2012 11:03 PM

Give us an example of the two consecutive lines _and_ an example of the result you want to accomplish.

the_rhino 12-09-2012 11:30 PM

What I have
<h2 id="b001c001" class="chapter">1</h2><p>

What I want
<h2 id="b001c001" class="chapter">1</h2><p>
<span id="b001c001v001" class="verse">1</span> I

The second line has more than just the one capital letter but the rest of the line isn't touched or changed. Like I said in my OP this works all day long in Vim very well. I would rather run it from the cli doing about 1200 pairs of lines within several dozen files.

pan64 12-10-2012 04:25 AM

here is a tutorial about multi line regex and sed:
otherwise perl is suggested, probably it handles this case better

this will do something similar (what you wish), but I'm not really sure:

perl -we '
undef $/;
while ( <> ) { s!
              ^([ ]*</p>)?                            # $1
              (<h2[ ]id=")                            # $2
              (b0[0-9][0-9]c[0-1][0-9][0-9])          # $3
              ("[ ]class="chapter">[0-9]+</h2><p>)    # $4
              \n                                      # $
              ([ ]*)                                  # $5
              ([A-Z])                                  # $6
              !$2$3$4\n$5<span id="$3v001" class="verse">1</span> $6
            }' inputfile

All times are GMT -5. The time now is 09:25 AM.