将格式化应用到 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将
tail -f
的输出通过管道传输到sed
中,并添加一些 ANSI 转义码。 例如,以下代码会将所有数字着色为红色(颜色 31),将所有带引号的字符串着色为亮黄色(颜色 93):Pipe the output of
tail -f
intosed
, 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):我认为您正在寻找某种
sed
脚本,它将用 ANSI 颜色转义序列包围您选择的单词。 (嗯...让我看看)。编辑 好的,明白了:
这是一个以深红色输出“Hello”的示例:
发生了什么事? 首先,我使用
echo -e
以便echo
不会将斜杠转换为屏幕上的斜杠,而是读取的转义序列\033
作为单个转义字符。 这实际上只是八进制的 33,即 27(ESC 键的序数)。因此,真正发送到屏幕的内容类似于:
ANSI 显示将其解释为“首先执行命令
32m
,输出“Hello”,然后执行命令
0m
。在本例中,命令
32m
表示“将前景颜色设置为2”,并且由于颜色#2是深红色,因此终端使用的“笔”现在将是深红色。 这意味着从此时开始,屏幕上显示的所有文本都将变为深红色。当我们输出完应该是红色的文本后,我们希望重置颜色,因此我们调用命令
0m
将颜色重置为正常。有关所有转义码的列表,请在 [http://en.wikipedia.org/wiki/ 中查找ANSI_escape_code 维基百科]或者只是谷歌搜索它。
因此,您的 sed 脚本所要做的就是将您选择的单词替换为颜色包围的单词。 例如,要替换
/var/log/messages
中的单词“Feb”,请执行以下操作:(您可能必须以 root 身份执行此操作才能实际读取
/var/log/messages
)sed 所做的就是搜索单词“Feb”,并用与我们上面使用的相同的转义序列包围它。
您可以将其扩展为多个单词着色:
将“Feb”、“Mar”、“Apr”着色为深红色。
我希望这能让您了解如何做您需要的事情!
I think what you're looking for is some kind of
sed
script that will surround the words you choose with ANSI Color escape sequences. (hmm... lemme look).EDIT OK, got it:
Here's an example for outputting "Hello" in dark red:
What's happening? First of all, I'm using
echo -e
so thatecho
doesn't convert the slashes into on-screen slashes, but rather reads the escape sequence of\033
as a single escaped character. This is actually just 33 in octal, which is 27 (the ordinal for the ESC key).So what is really being sent to the screen is something like:
Which the ANSI display interprets as "first do the command
32m
, output "Hello", and thendo the command
0m
.In this case, the command
32m
means "set the forground color to 2", and since color #2 is dark red, the "pen" used by the terminal will now be dark red. Which means that from this point onwards, all the text that will be displayed on screen will be dark red.When we're done outputting the text that supposed to be red, we wish to reset the colors, so we call the command
0m
which resets the colors to normal.For a list of all the escape codes, look up in [http://en.wikipedia.org/wiki/ANSI_escape_code Wikipedia] or just google for it.
So all your sed script has to do is replace the words you choose with the words surrounded by the colors. For example, to replace the word "Feb" in
/var/log/messages
, do the following:(You might have to do this as root to actually read
/var/log/messages
)All that
sed
did was search for the word "Feb" and surround it with the same escape sequence as we used above.You could expand it to color multiple words:
Which would color "Feb", "Mar", "Apr" - each in dark red.
I hope this gives you some idea of how to do what you need!
感谢 Scraimer 和 Adam,我想出了我想要的东西,我想我应该在这里分享给其他人:
这将:变成
这样(但带有一些颜色):
Thanks to both scraimer and Adam, I came up with pretty much what I was after, and I thought I'd share it here for anyone else:
Which turns this:
Into this (but with some colour):