Yagyavalk Bhatt
Yagyavalk Bhatt

Reputation: 342

Putting 2 consecutive lines of same matching pattern into single line

I want to parse this set of lines so that if get the same pattern like **<[email protected]>** in consecutive lines, it should print it in single with "," in between the two.

q2VDWKkY010407  2221878 Sat Mar 31 19:37 <Mailer-daemon>
                     (host map: lookup (my.local.domain): deferred)
                                             <[email protected]>
                                             <[email protected]>
q2VDWKkY010407  2221878 Sat Mar 31 19:37 <Mailer-daemon>
                     (host map: lookup (my.local.domain): deferred)
                                             <[email protected]>
                                             <[email protected]>

How can I do that in Perl?

Upvotes: 0

Views: 301

Answers (1)

flesk
flesk

Reputation: 7579

With lookaround:

#!/usr/bin/env perl
use strict;
use warnings;

my $str = <<'EOS';
q2VDWKkY010407  2221878 Sat Mar 31 19:37 <Mailer-daemon>
                     (host map: lookup (my.local.domain): deferred)
                                             <[email protected]>
                                             <[email protected]>
q2VDWKkY010407  2221878 Sat Mar 31 19:37 <Mailer-daemon>
                     (host map: lookup (my.local.domain): deferred)
                                             <[email protected]>
                                             <[email protected]>
EOS

$str =~ s/(?<=<yagyavalkbhatt\@yahoo.com>)\s+(?=<yagyavalkbhatt\@yahoo.com>)/,/g;
print $str;

Output:

q2VDWKkY010407  2221878 Sat Mar 31 19:37 <Mailer-daemon>
                 (host map: lookup (my.local.domain): deferred)
                                         <[email protected]>,<[email protected]>
q2VDWKkY010407  2221878 Sat Mar 31 19:37 <Mailer-daemon>
                 (host map: lookup (my.local.domain): deferred)
                                         <[email protected]>,<[email protected]>

Upvotes: 2

Related Questions