您可以为从文件中提取的特定行上色吗? [英] Can you colorize specific lines that are grepped from a file?

查看:101
本文介绍了您可以为从文件中提取的特定行上色吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每周运行CRONTAB,从40多个远程服务器收集硬件信息,并在家庭办公室的报表服务器上创建每周日志文件.我有一个针对该每周文件运行的脚本,仅将特定的状态行输出到我的显示器.

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

这是示例输出:

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
################################################

是否可以通过cat/grep/sed/awk/perl/此输出来使以CriticalYes结尾的行着色?

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?

推荐答案

感谢您的所有回复.我最终将原始grep结果传送到另一个grep | grep --color=auto '.*\(Yes\|Critical\).*\|$',并得到了想要的彩色结果:

Thank you for all of your responses. I ended up piping the original grep results to another grep | grep --color=auto '.*\(Yes\|Critical\).*\|$' and got the colorized results I wanted:

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

这是新的示例输出:

这篇关于您可以为从文件中提取的特定行上色吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆