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


i have script uses robocopy transfer files , write logs file "logfile.txt" after that, parse file "logfile.txt" further , skim necessary data , write other text file called "logfile_parsed.txt".my issue on here.initially calculate no of lines , parse each , every line ; whats goal when reach line matches word skipped , if line number x; append out lines (x-5) (x+1) new log file "logfile_parsed.txt". line talking below;

               total    copied   skipped  mismatch    failed    extras dirs :         1         1         0         0         0         0 

now , whwere stuck ; want append these lines parsed log fiel, when digit below line skipped or failed greater 0; i.e following ;

               total    copied   skipped  mismatch    failed    extras dirs :         1         1         1         0         1         0 

how can done? above 2 lines mentioned consistent throughout log file.how can know exact position of digit under skipped or failed , read it? please let me know valuable suggestions.

if understand correctly, want find line word "skipped" followed line number 1 in column below "skipped", , append 2 lines , 5 preceding lines new file?

  1. read logfile.txt array
  2. iterate through array searching lines "skipped"
  3. whenever find one, use regex match see if next line (i.e., next element of array) has 1 in corresponding position
  4. use array slice elements 5 preceding 1 following current one, , append new file

the following work if matching lines formatted in example:

$logfile = gc '<path>\logfile.txt' ($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 columns can vary in number , order, you'll need use capture groups find ordinal position of "skipped" , check if there 1 in corresponding position on next line. that's little more complicated, won't if sufficient.


Comments