G'day freekngeek,
I think this is probably the algorithm you're after but see notes at the end.
#!/usr/bin/env perl
use strict;
use warnings;
my $in_plus_lines = 0;
while (<DATA>) {
chomp;
if (/^\+(.*)$/) {
print "\n" unless $in_plus_lines;
$in_plus_lines = 1;
print $1;
}
else {
$in_plus_lines = 0;
print "\n" if $. > 1;
print "$_";
}
}
print "\n";
__DATA__
* asterisk_line_1
*
.dot_line
+ plus_line_1
+ plus_line_2
* asterisk_line_3
+ plus_line_3
+ plus_line_4
* asterisk_line_4
+ plus_line_5
+ plus_line_6
plain_text_line_1
+ plus_line_7
plain_text_line_2
+ plus_line_8
*
*asterisk_line_6
*asterisk_line_7
plain_text_line_3
Output:
$ pm_cat_plus_lines.pl
* asterisk_line_1
*
.dot_line
plus_line_1 plus_line_2
* asterisk_line_3
plus_line_3 plus_line_4
* asterisk_line_4
plus_line_5 plus_line_6
plain_text_line_1
plus_line_7
plain_text_line_2
plus_line_8
*
*asterisk_line_6
*asterisk_line_7
plain_text_line_3
Notes:
-
Your sample input data could be substantially reduced: quite a few lines at the beginning and end could be removed; the length of lines could also be reduced as they can become hard to read when concatenated (due to wrapping).
-
Expected output would have been useful. I don't believe your prosaic description of the wanted output is correct; e.g. "I want all the lines to be in one line which start with '+' sign" doesn't look right — I think you want groups of lines starting with '+', not all lines.
-
The differences in data in consecutive lines is hard to discern, e.g. it takes a fair amount of effort to tell "PC_M1_M2_75_500_75_500_75_500_75_500_75_bot" and "PC_M1_M2_75_100_75_100_75_100_75_100_75_bot" apart.
-
According to "and then removes the '+' sign", you end up with a leading space when the "+" lines are concatenated. Is that what you wanted? (Expected output would have made that obvious.)
As you can see, I've used completely different input; hopefully, the output is easier to read.
Given the points above, my algorithm may be incorrect: there may be enough for you to correct it for your needs; if not, please address those points before asking follow-up questions.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|