Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colour-coding get-content results

I've got a powershell script that monitors a log file, filters out the interesting bits and then presents those bits to me as and when they are written to the file. Works wonderfully. The line of interest is:

get-content "$logFile" -wait | where { select-string $searchTerm -inp $_ }

Now I want to get fancy!

I would like the font colour to change everytime a particular term is encountered. I can set the font colour easily enough, but how would you do it on-the-fly with the above statement?

Edit: Figured it out, but can't post an answer for 8 hours. Will upload it tomorrow.

like image 974
His Royal Redness Avatar asked May 25 '11 23:05

His Royal Redness


People also ask

Why color coding is important?

Color coding's main purpose is to separate and organize. This is especially important in the food industry where cross-contamination, allergen cross-contact, and cleaning chemical strength are all concerns.

What is the best color coding?

Use red for detail-oriented tasks. The University of British Columbia found that red “is the most effective at enhancing our attention to detail.” Red is the ideal color for coding tasks that deserve your undivided attention.

How many color codings are there?

With modern browsers supporting the full spectrum of 24-bit color, there are 16,777,216 different color possibilities.


2 Answers

If you're looking for something that provides selective color coding, then try something like this.

First, set up a helper function to select an appropriate color:

function Get-LogColor {
    Param([Parameter(Position=0)]
    [String]$LogEntry)

    process {
        if ($LogEntry.Contains("DEBUG")) {Return "Green"}
        elseif ($LogEntry.Contains("WARN")) {Return "Yellow"}
        elseif ($LogEntry.Contains("ERROR")) {Return "Red"}
        else {Return "White"}
    }
}

Then execute a line that looks like this:

gc -wait $logFile | ForEach {Write-Host -ForegroundColor (Get-LogColor $_) $_}
like image 107
StevoInco Avatar answered Oct 21 '22 02:10

StevoInco


Try

Get-Content $logFile -Wait |
  Select-String $searchTerm | 
  ForEach {write-host -ForegroundColor red $_.line}
like image 42
Doug Finke Avatar answered Oct 21 '22 01:10

Doug Finke