1

I want to perform some log analyses. Example log file listed below. I know how to resolve this issue with procedure/script, but I wonder how it can be done in 'powershell style' with pipelines etc

Log file contains from string with filename and list of components. Each of them can be 'passed' or 'failed'. I want to print all lines starting with 'Checking' which has at least one FAILED component.


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

At the glance looks like I need something similar to

gc logtxt | select-string -pattern 'FAILED' -context <nearest line starting with "Checking" in begining direction>,<lines count from CheckingLine to "LineWith 'FAILED'">
1
  • 1
    Is there an empty line between every "Checking"-entry or did you put the empty line there just to have cleaner code? Commented May 23, 2012 at 8:02

2 Answers 2

2

A crude version:

$line = $null
gc test.txt | %{ 
    if($line){ 
        if($_ -match "FAILED"){
            $line
            $line = $null
        }
    }
    if($_ -match "^Checking"){
        $line = $_
    } 
}
Sign up to request clarification or add additional context in comments.

Comments

0

Thanks for tips. Finally I got following working code:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.