Find lines that do not begin with "<", perform actions

I use vim and have a large text file containing some html abandoned in throoghout. I am trying to prepare it for the Internet and add <p></p> tags to strings that are not formatted yet. 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’m looking for a way to find lines that do not start with a < character, and for these lines add opening and closing <p></p> tags ... so that then 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 to find strings that do not match the start character < ?

+4
source share
5 answers
 ^([^<].*)$ 

Make sure that your options are forbidden "Duplicate newline" and replace with:

 <p>$1</p> 

Vim requires you to avoid certain characters, but I don't have a virtual vim, so this is my best guess for the whole rule:

 s:^\([^<].*\)$:<p>\1</p>:g 
+10
source

here is the logic. browse the file, check < at the beginning of the line, if not, create a new line with <p> and </p> and repeat it. No need for complex regex

with bash

 #!/bin/bash shopt -s extglob while read -r line do case "$line" in "<"*) echo $line ;; *) echo "<p>$line</p>";; esac done <"file" 

with awk

 $ awk '!/^</{$0="<p>"$0"</p>"}{print}' file 

Exit

 $ awk '!/^</{$0="<p>"$0"</p>"}1' file <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> 
+1
source
 :%s/^[^<].*/<p>&<\/p>/ 

as an alternative:

 :v/^</s#.*#<p>&</p># 

that is all that is needed.

+1
source

This should work:

 :%s/^\s*[^<]\+$/<p>&<\/p>/g 
0
source

Another way to do this:

 :v/^</normal I<p>^O$</p> 

^ O is done by pressing CTRL + o

Or if you use the surround.vim plugin:

 :v/^</normal yss<p> 
0
source

Source: https://habr.com/ru/post/1301343/


All Articles