Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I insert a line above specific lines in a file using Vim or Perl?

Tags:

regex

vim

sed

perl

I'd like to insert the line

<hr />

above every occurrence of a header 2 line in a file - e.g., above this pattern

<h2>variable pattern here</h2>

So the above should become

<hr />
<h2>variable pattern here</h2>

How can I do this with Vim, Sed or Perl?

like image 339
woollybrain Avatar asked Jan 15 '13 09:01

woollybrain


1 Answers

vim way:

cmd :g/<h2>/normal O<hr /> will do the job.

see it here: (I took the example from sudo_O)

enter image description here

like image 120
Kent Avatar answered Sep 25 '22 05:09

Kent