当然!
我根据8种颜色变量定义编写了一个名为“ egrepi”的函数。此功能仅通过管道传递,就像“ tail -f”彩色函数一样。
1. setColors
首先,首先要调用颜色变量函数:
setColors ()
{
set -a
which printf >/dev/null 2>&1 && print=printf || print=print # Mandriva doesn't know about printf
hide='eval tput civis'
show='eval tput cnorm'
CLS=$(tput clear)
bel=$(tput bel)
case ${UNAME} in
AIX)
# text / foreground
N=$(${print} '\033[1;30m')
n=$(${print} '\033[0;30m')
R=$(${print} '\033[1;31m')
r=$(${print} '\033[0;31m')
G=$(${print} '\033[1;32m')
g=$(${print} '\033[0;32m')
Y=$(${print} '\033[1;33m')
y=$(${print} '\033[0;33m')
B=$(${print} '\033[1;34m')
b=$(${print} '\033[0;34m')
M=$(${print} '\033[1;35m')
m=$(${print} '\033[0;35m')
C=$(${print} '\033[1;36m')
c=$(${print} '\033[0;36m')
W=$(${print} '\033[1;37m')
w=$(${print} '\033[0;37m')
END=$(${print} '\033[0m')
# background
RN=$(${print} '\033[6;40m')
Rn=$(${print} '\033[40m')
RR=$(${print} '\033[6;41m')
Rr=$(${print} '\033[41m')
RG=$(${print} '\033[6;42m')
Rg=$(${print} '\033[42m')
RY=$(${print} '\033[6;43m')
Ry=$(${print} '\033[43m')
RB=$(${print} '\033[6;44m')
Rb=$(${print} '\033[44m')
RM=$(${print} '\033[6;45m')
Rm=$(${print} '\033[45m')
RC=$(${print} '\033[6;46m')
Rc=$(${print} '\033[46m')
RW=$(${print} '\033[6;47m')
Rw=$(${print} '\033[47m')
HIGH=$(tput bold)
SMUL=$(tput smul)
RMUL=$(tput rmul)
BLINK=$(tput blink)
REVERSE=$(tput smso)
REVERSO=$(tput rmso)
;;
*)
# text / foreground
n=$(tput setaf 0)
r=$(tput setaf 1)
g=$(tput setaf 2)
y=$(tput setaf 3)
b=$(tput setaf 4)
m=$(tput setaf 5)
c=$(tput setaf 6)
w=$(tput setaf 7)
N=$(tput setaf 8)
R=$(tput setaf 9)
G=$(tput setaf 10)
Y=$(tput setaf 11)
B=$(tput setaf 12)
M=$(tput setaf 13)
C=$(tput setaf 14)
W=$(tput setaf 15)
END=$(tput sgr0)
HIGH=$(tput bold)
SMUL=$(tput smul)
RMUL=$(tput rmul)
BLINK=$(tput blink)
REVERSE=$(tput smso)
REVERSO=$(tput rmso)
# background
Rn=$(tput setab 0)
Rr=$(tput setab 1)
Rg=$(tput setab 2)
Ry=$(tput setab 3)
Rb=$(tput setab 4)
Rm=$(tput setab 5)
Rc=$(tput setab 6)
Rw=$(tput setab 7)
RN=$(tput setab 8)
RR=$(tput setab 9)
RG=$(tput setab 10)
RY=$(tput setab 11)
RB=$(tput setab 12)
RM=$(tput setab 13)
RC=$(tput setab 14)
RW=$(tput setab 15)
;;
esac
BLUEf=${B}
BLUE=${b}
REDf=${R}
RED=${r}
GREENf=${G}
GREEN=${g}
YELLOWf=${Y}
YELLOW=${y}
MANGENTAf=${M}
MANGENTA=${m}
WHITEf=${W}
WHITE=${w}
CYANf=${C}
CYAN=${c}
OK="${RG}${n}OK${END}"
KO="${RR}${n}KO${END}"
NA="${N}NA${END}"
COLORIZE='eval sed -e "s/{END}/${END}/g" -e "s/{HIGH}/${HIGH}/g" -e "s/{SMUL}/${SMUL}/g" -e "s/{RMUL}/${RMUL}/g" -e "s/{BLINK}/${BLINK}/g" -e "s/{REVERSE}/${REVERSE}/g" -e "s/{REVERSO}/${REVERSO}/g"'
LOWS=' -e "s/{n}/${n}/g" -e "s/{r}/${r}/g" -e "s/{g}/${g}/g" -e "s/{y}/${y}/g" -e "s/{b}/${b}/g" -e "s/{m}/${m}/g" -e "s/{c}/${c}/g" -e "s/{w}/${w}/g"'
HIGHS=' -e "s/{N}/${N}/g" -e "s/{R}/${R}/g" -e "s/{G}/${G}/g" -e "s/{Y}/${Y}/g" -e "s/{B}/${B}/g" -e "s/{M}/${M}/g" -e "s/{C}/${C}/g" -e "s/{W}/${W}/g"'
REVLOWS=' -e "s/{Rn}/${Rn}/g" -e "s/{Rr}/${Rr}/g" -e "s/{Rg}/${Rg}/g" -e "s/{Ry}/${Ry}/g" -e "s/{Rb}/${Rb}/g" -e "s/{Rm}/${Rm}/g" -e "s/{Rc}/${Rc}/g" -e "s/{Rw}/${Rw}/g"'
REVHIGHS=' -e "s/{RN}/${RN}/g" -e "s/{RR}/${RR}/g" -e "s/{RG}/${RG}/g" -e "s/{RY}/${RY}/g" -e "s/{RB}/${RB}/g" -e "s/{RM}/${RM}/g" -e "s/{RC}/${RC}/g" -e "s/{RW}/${RW}/g"'
# COLORIZE Usage:
# command |${COLORIZE} ${LOWS} ${HIGHS} ${REVLOWS} ${REVHIGHS}
set +a
}
2. egrepi
egrepi功能有效且优雅:在8种或更多颜色(您的需要)之间进行颜色循环,并在3种不同的unix操作系统下进行了测试,并带有注释:
# egrepi() egrep with 8 REVERSE cyclic colorations on regexps almost like egrep
# egrepi
# current script will work for KSH88, KSH93, bash 2+, zsh, under AIX / Linux / SunOS
egrepi ()
{
args=$*
# colorList=wBcgymrN # KSH93 or bash 3+, not for AIX
# set -A color # needed with older sh
color[0]=$Rw; color[1]=$RB; color[2]=$Rc; color[3]=$Rg; color[4]=$Ry; color[5]=$Rm; color[6]=$Rr; color[7]=$RN; # this is the only one AIX solution
i=0
unset argsToGrep argsSedColor argsPerlColor
for arg in ${args}
do
[ "${arg}" == "." ] && arg=\\. # if you wanna grep "."
# color=R${colorList:((${RANDOM: -1:1})):1} # bash RANDOMized colors
# color=R${colorList:$i:1} && let i++ && ((i==8)) && i=0 # KSH93 or bash 3+, not for AIX
argsToGrep="${argsToGrep}${argsToGrep:+|}${arg}"
# argsSedColor="${argsSedColor} -e s#${arg}#$n${!color}&${w}#gI" # AIX KSH88 do not recognise this fucking variable double expansion
# argsSedColor="${argsSedColor} -e s#${arg}#$n${color[$i]}&${w}#gI" # AIX neither do include sed with Ignore case
argsPerlColor="${argsPerlColor}${argsPerlColor:+,}s#${arg}#$n${color[$i]}$&${END}#gi" # So: gotta use perl
let i+=1 && ((i==8)) && i=0 # AIX KSH88 do not recognise "let i++"
done
# egrep -i "${argsToGrep}" | sed ${argsSedColor} | egrep -v "grep|sed" # AIX sed incompatibility with Ignore case
# (($# > 0)) && (egrep -i "${argsToGrep}" | perl -p -e ${argsPerlColor}) || cat # this line colors & grep the words, will NOT act as "tail -f"
(($# > 0)) && (perl -p -e ${argsPerlColor}) || cat # this line just colors the words
}
3.用法
命令 egrepi word1 .. wordN