将数组中的不同行写入两个不同的文件

时间:2014-11-06 16:32:09

标签: regex perl

我有一个命令的输出数组,如下所示

@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:

有一堆更相似的线条。 我的代码目前正在写一个文件:Text after Code:Message 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,条形文件信息如下

/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

1 个答案:

答案 0 :(得分:1)

您可以打开另一个文件并添加新的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;