I run a weekly CRONTAB that collects hardware info from 40+ remote servers and creates a weekly log file on our report server at the home office. I have a script that I run against this weekly file to output only specific status lines to my display.
#!/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 a sample 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 to cat / grep / sed / awk / perl / this output so that any lines that end with either Critical
or Yes
, get colorized?
With GNU grep:
grep --color -E ".*Yes$|.*Critical$|$" file
You could try ack, a very nice alternative to grep:
% ack '(Critical|Yes)$' file
# Or to colorize the whole line:
% ack '(.*(Critical|Yes))$' file
See Beyond grep
Or if you want to see all lines and only colorize specific ones:
use Term::ANSIColor qw/ colored /;
while (<$fh>) {
s/(.*)(Critical|Yes)$/colored(["yellow bold"], $1.$2)/e;
print;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With