我每周运行一次crontab,它收集信息并创建一个日志文件。
我有一个针对这个每周文件运行的脚本,它只将特定的状态行输出到我的显示器上。
#!/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这是一个示例输出:
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有没有一种方法可以对当前输出进行cat / grep / sed / awk / perl /,这样任何以Redundancy或Health开头但不以Full或Ok结尾的代码行都会被着色?
我想看到的是这个

我已经尝试通过管道将当前输出传输到| grep --color=auto \bRedundancy\w*\b(?<!Full)\|\bHealth\w*\b(?<!Ok),但没有成功。任何帮助都将不胜感激。
发布于 2018-01-24 06:09:54
在任何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

但是,对于字符串比较,您确实应该使用更健壮的表达式,而不是当前松散的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发布于 2018-01-24 05:13:21
使用GNU grep:
| grep -P --color=auto '^Redundancy.*(?<!Full)$|^Health.*(?<!Ok)$|$'-P表示使用PCRE进行后视(我不认为grep支持其他方式),|$表示输出所有行。您需要在行尾之前使用后视。
https://stackoverflow.com/questions/48410744
复制相似问题