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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |