Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find lines that don't begin with a "<", perform action

Tags:

regex

vim

sed

Am using vim and have a large text file that contains some html thrown in throoghout. Am trying to prepare it for the web and need to add <p></p> tags to the lines that are not yet formatted. Here is an example of what I have:

Paragraph text one one line [... more ... ]
Other paragraph text on the next line [... more ... ]  
<h1>html element thrown in on its own line</h1>
More paragraph text [... more ... ]  
<!-- some other element (always own line) -->
There is still more text!

I am looking for a way to search for the lines that don't begin with a < character and, for those lines, add opening and closing <p></p> tags ... so that, afterwards, my file resembles this:

<p>Paragraph text one one line [... more ... ] </p>
<p>Other paragraph text on the next line [... more ... ]   </p>
<h1>html element thrown in on its own line</h1>
<p>More paragraph text [... more ... ]   </p>
<!-- some other element (always own line ) -->
<p>There is still more text! </p>

How do I find lines that don't match a starting < character?

like image 391
thornomad Avatar asked Feb 15 '10 14:02

thornomad


1 Answers

^([^<].*)$

Make sure your options disallow "Dot matching newline" and replace with:

<p>$1</p>

Vim requires you to escape certain characters, but I don't actially have vim, so this is my best guess at the whole rule:

s:^\([^<].*\)$:<p>\1</p>:g
like image 12
John Gietzen Avatar answered Nov 06 '22 04:11

John Gietzen