linux - Is there a Bash equivalent to PowerShell's CmdLets, which act as a pipeline filter? -


i have program writing text outputs stdout. filter , color these outputs. in powershell, wrote cmdlet, parses text lines , emits them console , colors parts if needed. example

in powershell have such function:

function write-coloredprogline { [cmdletbinding()]   param(     [parameter(valuefrompipeline=$true)]     $inputobject,      [parameter(position=1)]     [switch]$suppresswarnings = $false,     [parameter(position=2)]     [string]$indent = ""   )    begin   { $errorrecordfound = $false  }    process   { if (-not $inputobject)     { write-host "empty pipeline!"  }     elseif ($inputobject -is [string])     { if ($inputobject.startswith("vlib "))       { write-host "${indent}$inputobject" -foregroundcolor gray   }  }       elseif ($inputobject.startswith("** warning:") -and -not $suppresswarnings)       { write-host "${indent}warning: " -nonewline -foregroundcolor yellow         write-host $inputobject.substring(12)       }       elseif ($inputobject.startswith("** error:") -and -not $suppresswarnings)       { $errorrecordfound += 1         write-host "${indent}warning: " -nonewline -foregroundcolor yellow         write-host $inputobject.substring(12)       }     }     else     { write-host "unsupported object in pipeline stream"   }   }    end   { $errorrecordfound   } } 

usage:

$expr = "prog.exe -flag -param foo" $errors = invoke-expression $expr | write-coloredprogline $false "  " 

how can process such operations in bash?

i need kind of inner state in filter script, tools grc not powerful enough.

here posix compatible way that:

awk ' /vlib/ {   $0 = "\33[1;36m" $0 "\33[m" } /warning/ {   $0 = "\33[1;33m" $0 "\33[m" } 1 ' 

result:

console screenshot

it should noted cannot use posix sed this. while tempting, posix sed has no way create escape sequences needed here, while posix awk does.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -