How to Parse a logfile in powershell and write out desired output

Darktux picture Darktux · Jul 19, 2013 · Viewed 11.6k times · Source

I have a script which uses robocopy to transfer files and write logs to a file "Logfile.txt" after that, i parse the file "LogFile.txt" further and skim the necessary data and write it to other text file called "LogFile_Parsed.Txt".My issue is over here.Initially i calculate no of lines and parse each and every line ; whats my goal is when i reach a line which matches the word skipped , if the line number is x; i will append out the lines (x-5) to (x+1) to the new log file "LogFile_Parsed.Txt". The line what i am talking about is below;

               Total    Copied   Skipped  Mismatch    FAILED    Extras
Dirs :         1         1         0         0         0         0

Now , whwere i am stuck is ; i only want to append these lines to parsed log fiel, when the digit below the line skipped or failed is greater than 0; i.e like following ;

               Total    Copied   Skipped  Mismatch    FAILED    Extras
Dirs :         1         1         1         0         1         0

How can it be done? the above 2 lines i mentioned are consistent throughout the log file.How can i know the exact position of digit under skipped or failed and read it? Please let me know your valuable suggestions.

Answer

Adi Inbar picture Adi Inbar · Jul 21, 2013

If I understand correctly, you want to find any line with the word "Skipped" followed by a line with the number 1 in the column below "Skipped", and append those two lines and the five preceding lines to a new file?

  1. Read LogFile.txt into an array
  2. Iterate through the array searching for lines with "Skipped"
  3. Whenever you find one, use a regex match to see if the next line (i.e., next element of the array) has a 1 in the corresponding position
  4. Use an array slice to get the elements from 5 preceding to 1 following the current one, and append it to the new file

The following will work if all the matching lines are formatted as in your example:

$logfile = gc '<path>\Logfile.txt'
for ($i = 0; $i -lt $logfile.count; $i++) {
    if ($logfile[$i] -match 'Skipped') {
        if ($logfile[$i + 1] -match '(?<=Dirs :(\s+[0-9]+){2}\s+)1') {
            $logfile[($i - 5)..($i + 1)] | Out-File -Append '<path>\Logfile_Parsed.txt'
        }
    }
}

If the columns can vary in number and order, you'll need to use capture groups to find the ordinal position of "Skipped" and check if there is a 1 in the corresponding position on the next line. That's a little more complicated, so I won't get into that if this is sufficient.