Reputation: 33
#+BEGIN_HTML
some html
more html
#+END_HTML
I have some files that contain text like the above. I want to remove any blank lines following #+BEGIN_HTML, so I end up with:
#+BEGIN_HTML
some html
more html
#+END_HTML
The file contains blank lines in other places, which I don't want to remove. I am struggling to come up with a regexp that will let me do this.
Upvotes: 1
Views: 81
Reputation: 74596
Here's another one using awk:
awk 'f{if(!NF)next;f=0}/#\+BEGIN_HTML/{f=1}1' file
f
is set to 1 when the pattern is matched. When f
is 1, blank lines (where NF
is 0) are skipped. f
is set to 0 when the line is no longer blank.
Testing it out:
$ cat file
#+BEGIN_HTML
some html
more html
#+END_HTML
$ awk 'f{if(!NF)next;f=0}/#\+BEGIN_HTML/{f=1}1' file
#+BEGIN_HTML
some html
more html
#+END_HTML
Upvotes: 0
Reputation: 41446
Here is an awk
solution:
awk '!NF && f {next} 1; NF {f=0} /#\+BEGIN_HTML/ {f=1}' file
#+BEGIN_HTML
some html
more html
#+END_HTML
Or as Jidder sugested:
awk '!NF&&f{next}{f=/#\+BEGIN_HTML/}1' file
Upvotes: 1
Reputation: 784928
Using awk
:
awk '/#\+BEGIN_HTML/{print; skip=1; next} skip {if (!NF) next; else skip=0} 1' file
#+BEGIN_HTML
some html
more html
#+END_HTML
Upvotes: 0
Reputation: 174696
Through perl,
$ perl -0777pe 's/(?<=^#\+BEGIN_HTML\n)\n+//g' file
#+BEGIN_HTML
some html
more html
#+END_HTML
OR
perl -00pe 's/(?<=^#\+BEGIN_HTML\n)\n+//g' file
Upvotes: 0
Reputation: 77075
Using sed
:
sed ':a;/^#+BEGIN_HTML/{N;/\n./!s/\n.*//;ta}' file
Create a label a
. For lines that start with #+BEGIN_HTML
, append the next line to pattern space. Substitute everything from newline to the end of next line unless it is not a line that is blank. Repeat the loop if the substitution modified the pattern space.
Upvotes: 1