将格式化应用到 unix shell

发布于 2024-07-13 13:02:37 字数 361 浏览 8 评论 0原文

我最近一直在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

打小就很酷 2024-07-20 13:02:37

tail -f 的输出通过管道传输到 sed 中,并添加一些 ANSI 转义码。 例如,以下代码会将所有数字着色为红色(颜色 31),将所有带引号的字符串着色为亮黄色(颜色 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"

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"
凹づ凸ル 2024-07-20 13:02:37

我认为您正在寻找某种 sed 脚本,它将用 ANSI 颜色转义序列包围您选择的单词。 (嗯...让我看看)。

编辑 好的,明白了:

这是一个以深红色输出“Hello”的示例:

echo -e "\033[31mHello\033[0m"

发生了什么事? 首先,我使用 echo -e 以便 echo 不会将斜杠转换为屏幕上的斜杠,而是读取 的转义序列\033 作为单个转义字符。 这实际上只是八进制的 33,即 27(ESC 键的序数)。

因此,真正发送到屏幕的内容类似于:

<ESC>[32mHello<ESC>[0m

ANSI 显示将其解释为“首先执行命令 32m,输出“Hello”,然后
执行命令0m

在本例中,命令32m表示“将前景颜色设置为2”,并且由于颜色#2是深红色,因此终端使用的“笔”现在将是深红色。 这意味着从此时开始,屏幕上显示的所有文本都将变为深红色。

当我们输出完应该是红色的文本后,我们希望重置颜色,因此我们调用命令 0m 将颜色重置为正常。

有关所有转义码的列表,请在 [http://en.wikipedia.org/wiki/ 中查找ANSI_escape_code 维基百科]或者只是谷歌搜索它。

因此,您的 sed 脚本所要做的就是将您选择的单词替换为颜色包围的单词。 例如,要替换 /var/log/messages 中的单词“Feb”,请执行以下操作:(

tail /var/log/messages | sed -e "s/Feb/\\o033[31m&\\o033[0m/"

您可能必须以 root 身份执行此操作才能实际读取 /var/log/messages)

sed 所做的就是搜索单词“Feb”,并用与我们上面使用的相同的转义序列包围它。

您可以将其扩展为多个单词着色:

tail /var/log/messages | sed -e "s/\(Feb\|Mar\|Apr\)/\\o033[31m&\\o033[0m/g"

将“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:

echo -e "\033[31mHello\033[0m"

What's happening? First of all, I'm using echo -e so that echo 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:

<ESC>[32mHello<ESC>[0m

Which the ANSI display interprets as "first do the command 32m, output "Hello", and then
do 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:

tail /var/log/messages | sed -e "s/Feb/\\o033[31m&\\o033[0m/"

(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:

tail /var/log/messages | sed -e "s/\(Feb\|Mar\|Apr\)/\\o033[31m&\\o033[0m/g"

Which would color "Feb", "Mar", "Apr" - each in dark red.

I hope this gives you some idea of how to do what you need!

旧时模样 2024-07-20 13:02:37

感谢 Scraimer 和 Adam,我想出了我想要的东西,我想我应该在这里分享给其他人:

RED=`echo -en '\e[31m'`
YELLOW=`echo -en '\e[93m'`
RESET=`echo -en '\e[00m'`

# line breaks here are just for formatting
tail -f ~/access-logs/access.log | sed -e "s@\([0-9\.]\+\) - - 
    \[[0-9]\+/[a-zA-Z]\+/[0-9]\+:\([0-9]\+:[0-9]\+:[0-9]\+\) +[0-9]\+] \"
    \(.\+\) HTTP/1\.[01]\" \([0-9]\+\) \([-0-9]\+\) \"\([^\"]\+\)\".*
    @\n\2 $YELLOW\4$RESET (\1)\n$RED\3$RESET\nBytes: \5\nFrom: \6@g"

# here's the full line if you wanted to copy it
# tail -f ~/access-logs/access.log | sed -e "s@\([0-9\.]\+\) - - \[[0-9]\+/[a-zA-Z]\+/[0-9]\+:\([0-9]\+:[0-9]\+:[0-9]\+\) +[0-9]\+] \"\(.\+\) HTTP/1\.[01]\" \([0-9]\+\) \([-0-9]\+\) \"\([^\"]\+\)\".*@\n\2 $YELLOW\4$RESET (\1)\n$RED\3$RESET\nBytes: \5\nFrom: \6@g"

这将:变成

182.108.1.20 - - [04/Feb/2009:23:24:41 +1000] "GET /images/exam_room.jpg HTTP/1.0" 200 8559 "http://www.myserver.com/courses/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"

这样(但带有一些颜色):

23:24:41 200 (182.108.1.20)
GET /images/exam_room.jpg
Bytes: 8559
From: http://www.myserver.com/courses/

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:

RED=`echo -en '\e[31m'`
YELLOW=`echo -en '\e[93m'`
RESET=`echo -en '\e[00m'`

# line breaks here are just for formatting
tail -f ~/access-logs/access.log | sed -e "s@\([0-9\.]\+\) - - 
    \[[0-9]\+/[a-zA-Z]\+/[0-9]\+:\([0-9]\+:[0-9]\+:[0-9]\+\) +[0-9]\+] \"
    \(.\+\) HTTP/1\.[01]\" \([0-9]\+\) \([-0-9]\+\) \"\([^\"]\+\)\".*
    @\n\2 $YELLOW\4$RESET (\1)\n$RED\3$RESET\nBytes: \5\nFrom: \6@g"

# here's the full line if you wanted to copy it
# tail -f ~/access-logs/access.log | sed -e "s@\([0-9\.]\+\) - - \[[0-9]\+/[a-zA-Z]\+/[0-9]\+:\([0-9]\+:[0-9]\+:[0-9]\+\) +[0-9]\+] \"\(.\+\) HTTP/1\.[01]\" \([0-9]\+\) \([-0-9]\+\) \"\([^\"]\+\)\".*@\n\2 $YELLOW\4$RESET (\1)\n$RED\3$RESET\nBytes: \5\nFrom: \6@g"

Which turns this:

182.108.1.20 - - [04/Feb/2009:23:24:41 +1000] "GET /images/exam_room.jpg HTTP/1.0" 200 8559 "http://www.myserver.com/courses/" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"

Into this (but with some colour):

23:24:41 200 (182.108.1.20)
GET /images/exam_room.jpg
Bytes: 8559
From: http://www.myserver.com/courses/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文