2012-05-23 34 views
1

我想执行一些日志分析。下面列出的示例日志文件。我知道如何解决程序/脚本这个问题,但我不知道它如何能在“PowerShell的风格”来完成与管道等具有动态上下文参数值的选择字符串

日志文件包含字符串的文件名和组件的列表。他们每个人都可以'通过'或'失败'。我想打印所有以'Checking'开头的行,它至少有一个FAILED组件。


Checking : C:\TFS\Datavarehus\Main\ETL\SSIS\DVH Project\APL_STG1_Daglig_Master.dtsx 

Checking : C:\TFS\Datavarehus\Main\ETL\SSIS\DVH Project\COPY_STG1_APL_Dekningsgrupper_Z1.dtsx 
    SQLCommand [Z1 APL_Dekningsgrupper] FAILED the VA1 check (table name as source) 
    SQLCommand [APL_Dekningsgrupper] passed the VA1 check 
    SQLCommand [FAK_Avkastningsgaranti_Kollektiv] FAILED the VA1 check (table name as source) 

Checking : C:\TFS\Datavarehus\Main\ETL\SSIS\DVH Project\DM_DVH_BpBedrift_InvesterteFond 1.dtsx 
    SQLCommand [DVH] passed the VA1 check 
    SQLCommand [Avtale Kurs] passed the VA1 check 

在骤眼看上去就像我需要类似

gc logtxt | select-string -pattern 'FAILED' -context <nearest line starting with "Checking" in begining direction>,<lines count from CheckingLine to "LineWith 'FAILED'"> 
+1

有每一个之间的空行“检查” -entry还是你把空行那里只是为了有更清晰的代码? –

回答

2

一些粗版本:

$line = $null 
gc test.txt | %{ 
    if($line){ 
     if($_ -match "FAILED"){ 
      $line 
      $line = $null 
     } 
    } 
    if($_ -match "^Checking"){ 
     $line = $_ 
    } 
} 
0

感谢提示。最后我得到了下面的工作代码:

gc test.txt | Foreach { 
    if ($_.StartsWith('Checking')) { 
     $pkname = $_ 
    } 
    if ($_.Contains('FAILED')) { 
     if (!($pkname.equals($printed_pkname))) { 
     $pkname; 
     $printed_pkname=$pkname} 
     $_ } 
    }