Putting 2 consecutive lines of same matching pattern into single line

后端 未结 1 420
-上瘾入骨i
-上瘾入骨i 2021-01-28 14:09

I want to parse this set of lines so that if get the same pattern like **** in consecutive lines, it should print it in single with

相关标签:
1条回答
  • 2021-01-28 14:34

    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)
                                                 <yagyavalkbhatt@yahoo.com>
                                                 <yagyavalkbhatt@yahoo.com>
    q2VDWKkY010407  2221878 Sat Mar 31 19:37 <Mailer-daemon>
                         (host map: lookup (my.local.domain): deferred)
                                                 <yagyavalkbhatt@yahoo.com>
                                                 <yagyavalkbhatt@yahoo.com>
    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)
                                             <yagyavalkbhatt@yahoo.com>,<yagyavalkbhatt@yahoo.com>
    q2VDWKkY010407  2221878 Sat Mar 31 19:37 <Mailer-daemon>
                     (host map: lookup (my.local.domain): deferred)
                                             <yagyavalkbhatt@yahoo.com>,<yagyavalkbhatt@yahoo.com>
    
    0 讨论(0)
提交回复
热议问题