2

I have a log file(.log) which is huge. I'm trying to find few keywords which need to be highlighted so that I can skip through other lines in the log faster. But, I don't want to filter only the select-string but highlight select-string. Also, I have no Idea about Powershell, I have the below command so far, from googling.

C:\users\proto> cat txtlog.log select-string "Valid" | write-host -foregroundcolor red

Which doesn't output what I want as it's only returning "valid" lines in red color but not the other lines.

5
  • 1
    A bit unclear on the output you want...example of source lines and desired output would help... Commented Mar 10, 2022 at 9:39
  • 1
    ValidLine 1 ValidLine 2 ValidLine 3 Someotherline x So If I run the above command I get all valid lines in red color but Someotherline x is not shown. So, I need a way to colour validlines and also show Someotherline without colour. Commented Mar 10, 2022 at 10:38
  • 1
    So you want the whole (huge) logfile displayed? You're likely to exceed screen buffer size... Commented Mar 10, 2022 at 18:39
  • 1
    Something like git diff --color-words would do this. I don't know how to do that with PowerShell. The Format-Color function suggested gets closer.
    – jla
    Commented Jul 27, 2023 at 16:57
  • Is it possible to Select-String by color?
    – phuclv
    Commented Jun 24 at 9:15

2 Answers 2

5

Add Format-Color function from here:

function Format-Color([hashtable] $Colors = @{}, [switch] $SimpleMatch) {
    $lines = ($input | Out-String) -replace "`r", "" -split "`n"
    foreach($line in $lines) {
        $color = ''
        foreach($pattern in $Colors.Keys){
            if(!$SimpleMatch -and $line -match $pattern) { $color = $Colors[$pattern] }
            elseif ($SimpleMatch -and $line -like $pattern) { $color = $Colors[$pattern] }
        }
        if($color) {
            Write-Host -ForegroundColor $color $line
        } else {
            Write-Host $line
        }
    }
}

Then, pipe your output to Format-Color:

cat txtlog.log | Format-Color @{ 'Valid' = 'Red' }

Lines with the word Valid will be displayed in red while the others will be displayed in the default color.

2
  • 1
    Is it not possible to run it in just a command? Like, without any functions... Commented Mar 10, 2022 at 10:37
  • 1
    @FirstTimeUser I don't think you can currently accomplish it without implementing your own function(s). Commented Mar 10, 2022 at 10:50
1

How about this???

$esc           = "$([char]27)"
$FormatWrapRed = "$esc[91m{0}$esc[33m"

txtlog.log | Get-Content | ForEach {
    If ($_ -match 'Valid') { $FormatWrapRed -f $_ }
    ELse {$_} 
}

Or (better IMHO) Add this filter definition to your profile:

Filter Wrap-Red {
    $esc = "$([char]27)"
    $WrapFormat = "$esc[91m{0}$esc[33m"
    If ($_ -match 'Valid') { $WrapFormat -f $_ }
    ELse {$_} 
}

Then in any console session, you can use:

txtlog.log | Get-Content | Wrap-Red

Or:

gc txtlog.log | Wrap-Red

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .