2012-06-04 87 views
0

我想解析這組行,以便在連續的行中獲得相同的模式,如**<[email protected]>**,它應該在兩行之間以「,」打印它。將連續兩行相同的匹配模式放入單行

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]> 

我該怎麼在Perl中做到這一點?

+0

人民反對票不指定原因。 – tuxuday

+0

發佈您的工作。因爲你還沒有發佈你的工作,所以我刪除了我的解決方案。 – tuxuday

回答

2

隨着環視:

#!/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; 

輸出:

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]> 
+0

謝謝flesk 但問題是你顯示的輸出可能會有所不同,如果連續第二個電子郵件ID是不同的和抱歉,但我不需要除了這些電子郵件地址以上的值 –

+0

@yagyavalkbhatt:你是說你只是想提取所有電子郵件地址從一個字符串?如果假設所有電子郵件地址都包含在'<' and '>'my @emails = $ str =〜/ <([^ <\@]+\@[^>] +)>/g;'應該完成。 – flesk

+0

Thanks Flesk 如何打印EOS或$ _ –