G'day jlope043,

Your match needs to capture the common and unique parts of each line. You can then output the common part once followed by all the unique parts joined with spaces. Here's the guts of what you need:

#!/usr/bin/env perl -l use strict; use warnings; my %reformat; my $re = qr{^(H\d+,\d+,)(.*)$}; while (<DATA>) { chomp; /$re/; push @{ $reformat{$1} }, $2; } print $_, join ' ', @{ $reformat{$_} } for keys %reformat; __DATA__ H123456,20151209,THIS IS A TEST H123456,20151209,TO COMBINE ALL H123456,20151209,MY MATCHING LINES H123456,20151209,INTO THE FIRST LINE H123456,20151209,THAT MATCHES. H654321,20151209,MATCH LINES FOR THIS H654321,20151209,ACCT INTO THE H654321,20151209,TOP LINE OF THE ACCT H432165,20151209,SINGLE LINE FOR THIS ONE

Output:

H123456,20151209,THIS IS A TEST TO COMBINE ALL MY MATCHING LINES INTO +THE FIRST LINE THAT MATCHES. H432165,20151209,SINGLE LINE FOR THIS ONE H654321,20151209,MATCH LINES FOR THIS ACCT INTO THE TOP LINE OF THE AC +CT

You may want some additional ordering to your output but I don't know what that is: sort by 'H' number value; the order that 'H' numbers appear; something else.

The code I've shown is very basic. You can probably find explanations for any part of it in perlintro; however, feel free to ask if you need further help.

— Ken


In reply to Re: Match Line And Combine Into One Line by kcott
in thread Match Line And Combine Into One Line by jlope043

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.