1

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.

1 Answer 1

1

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.

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

1 Comment

Thank you , it is working; i also found this poshcode.org/2164 which uses $tokens = [System.Management.Automation.PsParser]::Tokenize( $scriptContent, [ref] $null) | Where-Object { $ignoreTokens -notcontains $_.Type } $tokens = $tokens | Sort-Object StartLine,StartColumn But your solution looks better solution since it uses regex expressions.

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.