Thursday 15 April 2010

regex - Strip blank lines from first n lines using sed -


I need to strip empty rows only from the first 6 rows of a text file. I have a solution using a solution I have tried to mix it, but it has no use.

Here I am using the script ( faprep = '~ / misc-scripts / fa-prep) Sed ), the last command that is failing is:

  #! / Opt / local / bin / sed -f # title treatment s |  [B] [/ B] for Saga Titles | & Lt; \ (/ \? \) H2 [^ & gt;] * \? & Gt; | [\ 1i] | Change G # to & lt; H2 & gt; & Lt; / H2 & gt; [I] with [i] [/ i] for arc title. & Lt; / \? H3 [^ & gt;] * \? & Gt; G # Strip & lt; H3 id = "" & gt; & Lt; / H3 & gt; The title of chapter # HTML tag strips & amp; without removing text. Replacement s. & Lt; / \? P & gt; || The G # Strip All & lt; P & gt; & Lt; / P & gt; Tags | S | & Lt; \ (/ \? \ Em)> m [1] | Change G # to & lt; Em> & Lt; / Em> [I] [/ i] s | & Lt; \ (/ \? \) Strong> gt [| 1b] | Change G # to & lt; Strong & gt; & Lt; / Strong> For [B] [/ B] # serial code replacement / # 822 [01]; / \ "Replace / g # and # 8220; and # 8221; direct double quote (") S / & Amp; # 8217; / \ '/ G # Change & amp; # 8217; Straight Single Bid (') S / & amp; # 8230; Replace with /.../g # & amp; # 8230; With a 3-period ellipse (...) s / & amp; # 821 [12]; / - / g # Change & amp; # 8212; 2-hyphen m dash (-) # with the last presentation; & Lt; Body & gt; /, / & Lt; \ / Body & gt; /! D # Delete all & lt; Body & gt; & Lt; / Body & gt; Tag / & lt; \ / \? Body & gt; / D # Then, remove body tags: 3 # I note meeeeeeee !!!! 1,6 /. D} Remove the empty lines from around the # headings ??  

I am running from the terminal, this is the command, which shows the last line failing to bar the white space from the first 6 lines of the file (after all other modifications, the course ):

  calyodelphi @ dragonpad: ~ / pokemon-story / compilations $ ch = 'ch6'; Faprep $ ch-mmd.html & gt; $ CH-fa.txt; Head-6 $ ch-fa.txt [B] Hone Saga (S1) [/ B] [I] Next City Arc (A2) [/ i] Chapter 6: A peaceful city stroll ... or not calyodelphi @ Dragonpad: ~ / Pokemon-story / compilations $  

After the third title, the rest of the file is drawn from the blank line and then there are separate paragraphs from all empty rows. I want to put those empty rows, so that only blank lines are stripped between the titles at the top.

To clarify just a few points: Unix is ​​the end of the line in this file, and the lines are supposed to be empty space, even in a text editor that shows white space, By looking at, there is only one new row on each empty line.

Since the discussion in comments made it clear that you want to ignore the empty line in the first six lines In other words, in the second word, instead of the first six lines of input data, that part of the script has reached the first six times - you can not use the global line counter because you are not using edge buffer, we To make it your own counter Abuse can.

Therefore,

  1,6 / / /! D}  

with

  x # in grip buffer / swap .. {6}}! {# If it does not reach counter 6 s / / / / # increments in one (i.e., attach a letter) x # back swap input / D = # If it is empty, leave it x # Or else swap back} x # and swap once again This dance ensures that the # line from the input is in the pattern when we press # Leave out, even if # branches have been registered.  

Or, if it sounds too complicated, then use the suggestion of @Glenzackman and sed '1,6 / / /! Pipe the output of the script to the first time. D; } '' , because the second process will have itself line counter which are already working on processed data. There is no fun in it, but it will work.


No comments:

Post a Comment