0

I need to make a script that will find the "Error" in the log file and after this error copy its description, which is in a separate string. I manage to find and copy the entire error string, but it turns out that the block with the error description is located a little lower and I can not copy it.

Select-String C:\TEMP\Skript\Log.xml -Pattern ' level="ERROR" '  -AllMatches  |  select | out-file C:\TEMP\Skript\LogErrors.xml -append 

How do I specify additional parameters for a search?

The comment is in a separate block and not on the same line with the error. This block is not necessarily 1 line long.

<log4j:event logger="COMP" timestamp="1579174503853" level="ERROR" thread="1"><log4j:message>
here comes
a long error discription 
for a few lines.
</log4j:message>  

This is where the error description ends. So i must find ERROR in log and then copy this string and the description.

Expected Result: Get an every error with its description, even if the error description takes several lines.

   <log4j:event logger="COMP" timestamp="1579174503853" level="ERROR" thread="1"><log4j:message>
here comes a long error discription  for a few lines.
 </log4j:message> 
4
  • 2
    "located a little lower", you mean on the next line? Use -Context 0,1 with Select-String to grab one line after the match Commented Jan 17, 2020 at 11:10
  • I mean, the comment is in a separate block and not on the same line with the error. This block is not necessarily 1 line long. <log4j:event logger="COMP" timestamp="1579174503853" level="ERROR" thread="1"><log4j:message> here comes a long error discription for a few lines. </log4j:message> This is where the error description ends. So i must find ERROR in log and then copy this string and the description. Commented Jan 17, 2020 at 11:49
  • You could use the same approach I suggested in another question here. Commented Jan 17, 2020 at 11:59
  • Please update your question (don't use comments) to show sample input and expected output. Commented Jan 18, 2020 at 15:32

1 Answer 1

0

Considering the information in file Log.xml is:

    <log4j:event logger="COMP" timestamp="1579174503853" level="INFO" thread="1"><log4j:message>
    Information message
    </log4j:message>
    <log4j:event logger="COMP" timestamp="1579174503853" level="ERROR" thread="1"><log4j:message>
    here comes
    a long error discription 
    for three few lines.
    </log4j:message>
    <log4j:event logger="COMP" timestamp="1579174503853" level="INFO" thread="1"><log4j:message>
    Information message
    </log4j:message>
    <log4j:event logger="COMP" timestamp="1579174503855" level="ERROR" thread="1"><log4j:message>
    here comes a second long error discription 
    for a few lines.
    </log4j:message>
    <log4j:event logger="COMP" timestamp="1679174503855" level="ERROR" thread="1"><log4j:message>
    single line error. 
    </log4j:message>
    <log4j:event logger="COMP" timestamp="1579174503853" level="INFO" thread="1"><log4j:message>
    Information message
    </log4j:message>

You can use the next command

    Get-Content .\Log.xml | foreach {
    if ($_.Contains('level="ERROR"')) {
    $errorFound= $true }
    if ($errorFound -And $_.StartsWith('<l')) { 
    $linea =""; $_ } 
    elseif($errorFound -And $_.StartsWith('</')) { 
    $linea + "`n" + $_ ; $errorFound = $false; } 
    elseif ($errorFound) { 
    $linea = $linea +  $_  }
    } | Add-Content LogError.xml

This will save the information with the format you want in the file LogError.xml

Sign up to request clarification or add additional context in comments.

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.