LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   sed search and replace question (https://www.linuxquestions.org/questions/linux-general-1/sed-search-and-replace-question-4175440733/)

the_rhino 12-09-2012 09: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.

Code:

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 10: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 10:30 PM

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

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 03:25 AM

here is a tutorial about multi line regex and sed: http://www.grymoire.com/Unix/Sed.html#uh-51
otherwise perl is suggested, probably it handles this case better

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

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
              !msx;
              print;
            }' inputfile



All times are GMT -5. The time now is 03:15 AM.