将格式套用至Unix Shell [英] Apply formatting to unix shell

查看:72
本文介绍了将格式套用至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.

推荐答案

tail -f的输出放入sed,并添加一些

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屋!

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