简体   繁体   English

如何为以string1开头但不以string2结尾的行着色

[英]How do you color lines that begin with string1 but do not end with string2

I run a weekly crontab that collects information and creates a log file. 我运行每周一次的crontab,它收集信息并创建一个日志文件。

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
HW_FILE="$(ls -t /home/user/hwinfo/|head -1)"

# List the Site name, hardware group, Redundancy or Health status', and the site divider
grep -i 'Site\|^\*\*\|^Redundancy\|^Health\|^##' /home/user/hwinfo/$HW_FILE
echo "/home/user/hwinfo/"$HW_FILE
exit 0

This is a sample output: 这是一个示例输出:

Accessing Site: site01
** FANS **
Redundancy Status : Full
** MEMORY **
Health : Ok
** CPUs **
Health : Ok
** POWER SUPPLIES **
Redundancy Status : Full
##########################################
Accessing Site: site02
** FANS **
Redundancy Status : Full
** MEMORY **
Health : Degraded
** CPUs **
Health : Ok
** POWER SUPPLIES **
Redundancy Status : Full
##########################################
Accessing Site: site03
** FANS **
Redundancy Status : Failed
** MEMORY **
Health : Ok
** CPUs **
Health : Ok
** POWER SUPPLIES **
Redundancy Status : Full
##########################################
/home/user/hwinfo/hwinfo_102217_034001.txt

Is there a way to cat / grep / sed / awk / perl / the current output so that any lines that begin with either Redundancy or Health , but don't end in Full or Ok , respectively, get colorized? 有没有办法cat / grep / sed / awk / perl /当前输出,以便任何以RedundancyHealth开头但不以FullOk结尾的行都会变色?

What I want to see is this 我想看到的是这个

imgur链接

I've tried piping the current output to | grep --color=auto \\bRedundancy\\w*\\b(?<!Full)\\|\\bHealth\\w*\\b(?<!Ok) 我已经尝试将当前输出传递给| grep --color=auto \\bRedundancy\\w*\\b(?<!Full)\\|\\bHealth\\w*\\b(?<!Ok) | grep --color=auto \\bRedundancy\\w*\\b(?<!Full)\\|\\bHealth\\w*\\b(?<!Ok) without success. | grep --color=auto \\bRedundancy\\w*\\b(?<!Full)\\|\\bHealth\\w*\\b(?<!Ok)没有成功。 Any assistance would be greatly appreciated. 任何帮助将不胜感激。

With any awk in any shell on any UNIX box: 在任何UNIX机器上的任何shell中都有任何awk:

awk -v on="$(tput setaf 1)" -v off="$(tput sgr0)" '$1~/^(Health|Redundancy)$/ && $NF!~/^(Full|Ok)$/{$0 = on $0 off} 1'  file

在此输入图像描述

You should really use a more robust expression with string comparisons though rather than the current loose regexp: 你应该使用更强大的表达式与字符串比较,而不是当前松散的正则表达式:

awk -v on="$(tput setaf 1)" -v off="$(tput sgr0)" '
(($1=="Health") && ($NF!="Ok")) || (($1=="Redundancy") && ($NF!="Full")) { $0 = on $0 off }
1'  file

Using GNU grep: 使用GNU grep:

| grep -P --color=auto '^Redundancy.*(?<!Full)$|^Health.*(?<!Ok)$|$'

-P to use PCRE for lookbehind (which I don't think grep supports otherwise), |$ to make it output all lines. -P使用PCRE进行lookbehind(我不认为grep会支持), |$使它输出所有行。 You need to use the lookbehind right before the line-end. 您需要在行尾之前使用lookbehind。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM