将格式套用至Unix Shell [英] Apply formatting to unix shell
问题描述
最近我一直在使用tail -f
查看一些服务器日志,并且认为如果可以格式化输出,查看某些内容会容易得多.我真正要寻找的是一种为某些单词加上颜色(由正则表达式确定)并删除某些单词(同样由正则表达式确定)的方法.
I've been looking at some server logs using tail -f
recently, and have thought that it'd be much easier to see some things if I could format the output. Really all I'm looking for is a way to perhaps colour certain words (determined by a regex), and perhaps remove certain words (again, determined by a regex).
我知道
I know there's programs which visualize server logs in real time and whatnot, but I'm more interested in this.
推荐答案
Pipe the output of tail -f
into sed
, and add in some ANSI escape codes. For example, the following will colorize all numbers in red (color 31) and all quoted strings in bright yellow (color 93):
RED=`echo -en '\e[31m'`
YELLOW=`echo -en '\e[93m'`
RESET=`echo -en '\e[00m'`
tail -f file | sed -E "s/([0-9]+)/$RED\1$RESET/g;s/(\"[^\"]*\")/$YELLOW\1$RESET/g"
这篇关于将格式套用至Unix Shell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!