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.
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?
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.