写不同线从阵列到两个不同的文件

问题描述:

我有从命令输出阵列如下写不同线从阵列到两个不同的文件

@flows = 

-------- 
Code: Message id 'com.test.mb.TestHarness' on execution group 'GENERALEG' is running. 

Additional thread instances: '0' 
Deployed: '6/4/13 9:54 AM' in Bar file '/home/test/deploy/GENERALEG/TestHarness_2013-06-04_09-54- 30.bar' 
Last edited: '6/4/13 9:53 AM'. 
Long description: '' 
User-defined property names: 
Keywords: 
-------- 
Code: Message id 'com.test.mb.TestHarness1' on execution group 'GENERALEG' is running. 

Additional thread instances: '0' 
Deployed: '10/5/13 9:56 AM' in Bar file '/home/test/deploy/GENERALEG/TestHarness1_2013-10-05_09-56-30.bar' 
Last edited: '10/5/13 9:55 AM'. 
Long description: '' 
User-defined property names: 
Keywords: 
-------- 
Code: Message id 'com.cims.utility.Test' on execution group 'GENERALEG' is stopped. 

Additional thread instances: '0' 
Deployed: '5/20/13 4:27 AM' in Bar file '/home/test/deploy/GENERALEG/TestDecodeDEV_2013-05-20_04-27-53.bar' 
Last edited: '5/20/13 2:55 PM' 
Long description: '' 
User-defined property names: 
Keywords: 

有一堆更类似的路线。 我的代码当前正在写入一个文件后代码文本:消息ID'并采取它的状态。 我的代码

open $file, '>>', "$LogDir/snglflows.txt"; 
foreach $_ (@flows) 
{ 
next if /^(\s)*$/; 
if (/Code: Message id '(.*?)' .* running/) 
{ 
    print $file "$1,started\n"; 
} 
elsif (/Code: Message id '(.*?)' .* stopped/) 
{ 
print $file "$1,stopped\n"; 
} 
} 
close $file; 

但这段代码是给我输出到snglflows.txt文件如下

com.test.mb.TestHarness,started 
com.test.mb.TestHarness1,started 
com.cims.utility.Test,stopped 

有没有办法可以得到两个文件。一个snglflows.txt文件和其他人喜欢flowbars.txt与bar文件信息如下

/home/test/deploy/GENERALEG/TestHarness_2013-06-04_09-54-30.bar 
/home/test/deploy/GENERALEG/TestHarness1_2013-10-05_09-56-30.bar 
/home/test/deploy/GENERALEG/TestDecode_2013-05-20_04-27-53.bar 
+0

你可以做的更清楚。你在每个文件中需要什么? – Sobrique 2014-11-06 19:52:12

+0

在一个文件中,snglflows.txt我需要为'com.test.mb.TestHarness输出,开始 com.test.mb.TestHarness1,开始 com.cims.utility.Test,stopped' 在其他文件中, flowbars.txt我需要输出为'/home/test/deploy/GENERALEG/TestHarness_2013-06-04_09-54-30.bar /home/test/deploy/GENERALEG/TestHarness1_2013-10-05_09-56-30.bar /home/test/deploy/GENERALEG/TestDecode_2013-05-20_04-27-53.bar' – user3164754 2014-11-06 20:32:13

您可以打开另一个文件,添加一个新的ELSIF。

open $file, '>>', "$LogDir/snglflows.txt"; 
open $file2, '>>', "$LogDir/flowbars.txt"; 
foreach $_ (@flows) 
{ 
    next if /^(\s)*$/; 
    if (/Code: Message id '(.*?)' .* running/) 
    { 
      print $file "$1,started\n"; 
    } 
    elsif (/Code: Message id '(.*?)' .* stopped/) 
     { 
      print $file "$1,stopped\n"; 
    } 
    elsif (/Deployed: .* '(.*?)') 
    { 
      print $file2 "$1"; 
    } 
} 
close $file; 
close $file2; 
+0

我很想试着做一个多行的正则表达式来解析记录,但在这种情况下可能没有必要。 – Sobrique 2014-11-07 13:19:45

+0

我从来没有使用多行正则表达式,所以我没有与他们的经验。 – ProfGhost 2014-11-07 13:33:42