Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.

-- Ken


In reply to Re: Editing text file by kcott
in thread Editing text file by freekngeek

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-19 20:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found