Can you colorize certain lines that were copied from a file?

I run the weekly CRONTAB, which collects hardware information from 40+ remote servers and creates a weekly log file on our report server in the home office. I have a script that I run against this weekly file to display only certain status bars.

#!/bin/sh

# store newest filename to variable
DD_FILE="$(ls -t /home/user/ddinfo/|head -1)"

# List the site name, disk ID (virtual & physical), Status and State of each ID, Failure Prediction for each physical disk, and the site divider
grep -w 'Site\|^ID\|^State\|^Status\|^Failure Predicted\|^##' /home/user/ddinfo/$DD_FILE
echo "/home/user/ddinfo/"$DD_FILE
exit 0

This is an example output:

Accessing Site: site01
ID                            : 0
Status                        : Ok
State                         : Ready
ID                              : 0:0:0
Status                          : Ok
State                           : Online
Failure Predicted               : No
ID                              : 0:0:1
Status                          : Ok
State                           : Online
Failure Predicted               : No
################################################
Accessing Site: site02
ID                            : 0
Status                        : Ok
State                         : Ready
ID                              : 0:0:0
Status                          : Non-Critical
State                           : Online
Failure Predicted               : Yes
ID                              : 0:0:1
Status                          : Ok
State                           : Online
Failure Predicted               : No
################################################

Is there a way for cat / grep / sed / awk / perl / this output so that all lines ending with Criticalor Yesare colored?

+4
source share
5 answers

. grep grep | grep --color=auto '.*\(Yes\|Critical\).*\|$' , :

grep -i 'site\|^ID\|^State\|^Status\|^Failure Predicted\|^##' /home/user/ddinfo/$DD_FILE | grep --color=auto '.*\(Yes\|Critical\).*\|$'

:

Sample output

0

GNU grep:

grep --color -E ".*Yes$|.*Critical$|$" file
+9

, a grep:

% ack '(Critical|Yes)$' file
# Or to colorize the whole line:
% ack '(.*(Critical|Yes))$' file

. Beyond grep

, :

use Term::ANSIColor qw/ colored /;
while (<$fh>) {
    s/(.*)(Critical|Yes)$/colored(["yellow bold"], $1.$2)/e;
    print;
}
+4

, , Critical Yes, , :

awk -v on="$(tput smso)" -v off="$(tput rmso)" '/(Critical|Yes)$/{$0=on $0 off} 1' logfile

, , tput. smso/rmso reset " ". , tput.

, , "standout mode":

awk -v on="$(tput setaf 1)" -v off="$(tput sgr0)" '/(Critical|Yes)$/{$0=on $0 off} 1' logfile

tput setaf 1 - . ( tput - 1, - 2 ..). tput sgr0 - .

  • -v on="$(tput smso)" -v off="$(tput rmso)"

    awk-, on off, , .

  • /(Critical|Yes)$/{$0=on $0 off}

    , Critical Yes, on off .

  • 1

    awk . ​​

+4

Term::ANSIColor Perl:

... | perl -pne 'BEGIN { use Term::ANSIColor } /: (Yes|Critical)$/ && { $_ = color("red") . "$_" . color("reset") }'
+2

Source: https://habr.com/ru/post/1689923/


All Articles