Search code examples
powershellpowershell-5.0powershell-7.0

How to parse a table in TXT


I have a bunch of Log files (saved as .txt) that contain a 'table' at the end of each log that I need to parse.

What do I mean by 'table'?

            Total    Copied   Skipped  Mismatch    FAILED    Extras
 Dirs :       332       332       332         0         0         0
Files :      1351      1087       264         0         0         0
Bytes :   1.621 g   1.222 g  408.39 m         0         0         0

I need to search each of these text files for any entries under the 'failed' header in the 'table'.

I am, at best, at an intermediate level with authoring and use of PowerShell but I know nothing about any other scripting languages.

How can this be done using PowerShell v. 5+?

I have searched online including StackOverflow but I think I may be using the wrong terminology for what I need to do.


Solution

  • Your question is unclear but assuming you have already accomplished the task of extracting those tables from your files, the following replacement pattern would work to convert the tables into CSVs then you can use ConvertFrom-Csv and treat them as objects. For instance, using the table in question:

    $csv = @'
                Total    Copied   Skipped  Mismatch    FAILED    Extras
     Dirs :       332       332       332         0         0         0
    Files :      1351      1087       264         0         0         0
    Bytes :   1.621 g   1.222 g  408.39 m         0         0         0
    '@ -replace '(?: :)? {2,}', ',' | ConvertFrom-Csv -WA 0
    

    This would result into the following array of objects:

    H1    Total   Copied  Skipped  Mismatch FAILED Extras
    --    -----   ------  -------  -------- ------ ------
    Dirs  332     332     332      0        0      0
    Files 1351    1087    264      0        0      0
    Bytes 1.621 g 1.222 g 408.39 m 0        0      0
    

    Then you can simply filter or select as with any array of objects in PowerShell:

    PS ..\pwsh> $csv | Select-Object FAILED
    
    FAILED      
    ------      
    0
    0
    0
    
    PS ..\pwsh> $csv | Where-Object H1 -EQ Dirs
    
    H1       : Dirs
    Total    : 332
    Copied   : 332
    Skipped  : 332
    Mismatch : 0
    FAILED   : 0
    Extras   : 0
    

    Regex details can be found here: https://regex101.com/r/miBU2f/1.