Text Manipulation using PowerShell

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Amar Helloween Amar Helloween 1 month, 2 weeks ago.

  • Author
    Posts
  • #55955
    Profile photo of Amar Helloween
    Amar Helloween
    Participant

    Hi All,

    I have some data in a text file like below :

    My requirement is to Find the number of ROWS present between "Daily Load Statistics" and "Total Snags/Duplicates/Dishonors in Database" . For example we can see there are 5 rows (09-AUG-16) so the output should come as 5 Rows. After getting the ROWS Count,I have to check the number of OK's in that rows. it may be OK or Not OK.

    Final Output expected :
    ———————-
    Rows Count : 5
    No of OK : 4

    Kindly let me know if there is any queries

  • #55963
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    That might be oversized but I think it does the job:

    $SampleTxt = Get-Content -Path C:\Test\Sample.txt
    $ParsedResults = foreach ($Line in $SampleTxt) {
    	If($Line -match '(\d{2}-[A-Z]{3}-\d{2})\s+(\d{4})\s+(\w*)\s+(L)\s+(\d*)\s+(\d*\.\d*)\s+(\d*)\s+(\d*\.\d*)\s+([A-Z]+)' ){
            [PSCustomObject]@{
                'LOAD_DATE' = $Matches[1]
                'CYCLE_NO' = $Matches[2]
                'TABLE_NAME' = $Matches[3]
                'T' = $Matches[4]
                'EDIRIS_COUNT' = $Matches[5]
                'EDIRIS_AMOUNT' = $Matches[6]
                'CPC_COUNT' = $Matches[7]
                'CPC_AMOUNT' = $Matches[8]
                'STATUS' = $Matches[9]
            }
        }
    }
    $ParsedResults | Format-Table -AutoSize
    $RowsCount = $ParsedResults.Count
    $OKCount = ($ParsedResults | ? -Property 'Status' -Value 'OK' -EQ).Count
    Write-Host "Rows Count: $RowsCount"
    Write-Host "No of OK: $OKCount"
    

    Of course you have to adjust the path for your environment/needs.

  • #56008
    Profile photo of Amar Helloween
    Amar Helloween
    Participant

    Thanks, it was perfect. 🙂

You must be logged in to reply to this topic.