Artur Szymczak
Artur Szymczak

Reputation: 135

Copying text from one line to another

I have example file:

<i>text</i>
verse 19 3 1 text2
verse 19 3 2 text3
<i>text4</i>
verse 19 4 1 text5
verse 19 4 1 text6
verse 19 4 1 text7

And I need to convert it to:

pericope 19 3 1 <i>text</i>
verse 19 3 1 text2
verse 19 3 2 text3
pericope 19 4 1 <i>text4</i>
verse 19 4 1 text5
verse 19 4 1 text6
verse 19 4 1 text7

How to build regex for this?

Upvotes: 0

Views: 50

Answers (2)

LLaSmith
LLaSmith

Reputation: 11

Assuming you're doing this in vim and taking the numbers from the first line after the text tag then this regex works:

:%s/\(<i>text\d\?<\/i>\n\)\(verse\(\(\s\+\d\+\)\{3}\)\)/periscope\3 \1\2 

Upvotes: 0

anubhava
anubhava

Reputation: 784998

You can use awk:

awk '/<i>/{p=$0;next} p{s=$0; sub(/ +[^ ]+$/, "", s); print "pericope", s, p; p=""} 1' file
pericope verse 19 3 1 <i>text</i>
verse 19 3 1 text2
verse 19 3 2 text3
pericope verse 19 4 1 <i>text4</i>
verse 19 4 1 text5
verse 19 4 1 text6
verse 19 4 1 text7

Explanation:

  • Save line in variable p when you get a line with <i>
  • From next line strip last field and print "pericope", s, p
  • Initialize p to ""
  • Print next line using default action 1

Upvotes: 1

Related Questions