从一个文件匹配Perl模式并写入另一个文件

时间:2013-12-04 16:28:43

标签: perl

我有一个如下文字文件:

1.temp _result runCount = 0
start testCaseCosqSched::testCosqSched_ext_lossless_sched_mixed_001
XXXetcXXX
XXXetcXXX
XXXetcXXX
XXXetcXXX
XXXetcXXX
XXXetcXXX
Resetting Statistics ...
end   testCaseCosqSched::testCosqSched_ext_lossless_sched_mixed_001
time  testCaseCosqSched::testCosqSched_ext_lossless_sched_mixed_001 elapsed 46.968
PASS  testCaseCosqSched::testCosqSched_ext_lossless_sched_mixed_001 
start testCaseCosqSched::testCosqSched_ext_lossless_sched_update_001
XXXetcXXX
XXXetcXXX
XXXetcXXX
XXXetcXXX
XXXetcXXX
XXXetcXXX
Resetting Statistics ...
end   testCaseCosqSched::testCosqSched_ext_lossless_sched_update_001
FAIL testCaseCosqSched::testCosqSched_ext_lossless_sched_update_001 targetCosq::CosqSchedConfig (0, 3, 7, sched_mode=WRR, sched_weight=1) expected PASS but was     bcm_cosq_gport_sched_set(0, 3, 7, 3, 1) expected 0 but was -9..
start testCaseCosqSched::testCosqSched_ext_lossless_sched_update_002
XXXetcXXX
XXXetcXXX
XXXetcXXX
XXXetcXXX
XXXetcXXX
XXXetcXXX
Resetting Statistics ...
end   testCaseCosqSched::testCosqSched_ext_lossless_sched_update_002
time  testCaseCosqSched::testCosqSched_ext_lossless_sched_update_002 elapsed 100.078
PASS  testCaseCosqSched::testCosqSched_ext_lossless_sched_update_002

我需要打印为:

Count TestCase                          Verdit

1     testCaseCosqSched::testCosqSched_ext_lossless_sched_mixed_001              PASS

2     testCaseCosqSched::testCosqSched_ext_lossless_sched_update_001             FAIL

3     testCaseCosqSched::testCosqSched_ext_lossless_sched_update_002             PASS

有人可以建议一些如何做到的提示吗?

1 个答案:

答案 0 :(得分:1)

perl -ne 'printf "%-5d  %-70s  %s\n", ++$i, $2, $1 if m/^(PASS|FAIL)\s+(\S+)/' data

由于测试名称长度超过60个字符,因此需要为它们分配足够的空间。

输出:

1      testCaseCosqSched::testCosqSched_ext_lossless_sched_mixed_001            PASS
2      testCaseCosqSched::testCosqSched_ext_lossless_sched_update_001           FAIL
3      testCaseCosqSched::testCosqSched_ext_lossless_sched_update_002           PASS

不完全是Perl火箭科学。