in reply to How to remove white space and more than a particular number of new lines!
Without being a code-a-matic for you either, here’s a classic treatment of this sort of problem which can be done by Perl, or, perhaps even more simply, by awk:
There are two kinds of lines here: those that are blank, and those that are not. (A blank line would match something like /^\s*$/ ...) If the line is not blank, then you want to output that line and set a counter of the number of blank-lines encountered to zero. If it is, you want to increment that count, and output the line only if the count is so-far two or less. You must initialize that counter to zero before the loop begins. With this algorithm, a gap of one line would stand, but a gap of more than two lines would be two.
In data-processing in general, we all encounter a lot of “text-file problems” which can be reduced to the same basic idea: what different types of lines might I encounter, how do I recognize them, and, having recognized them, what do I do with them. (Also, what do I do at the beginning of the file and what do I do at the end.) Well, the awk tool is specifically designed around that very idea, and the Perl programming language was originally birthed as an über-extension of awk ... both for good reasons.
Now, I’m sure that you can see your way to writing that very-short program based on this?
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to remove white space and more than a particular number of new lines!
by Anonymous Monk on Apr 13, 2014 at 18:30 UTC | |
by graff (Chancellor) on Apr 14, 2014 at 02:06 UTC | |
by Anonymous Monk on Apr 14, 2014 at 03:53 UTC | |
by choroba (Cardinal) on Apr 14, 2014 at 07:33 UTC | |
by Anonymous Monk on Apr 15, 2014 at 17:10 UTC |