匹配“粗体”的转义序列在控制台输出中使用 grep

发布于 2024-09-17 23:53:16 字数 549 浏览 4 评论 0原文

你好,我有很多日志文件,其中包含 ^[[1m (如 vim 显示的那样)。我想通过观看日志文件的生活

tail -n 1000 -f logfile.log | grep <expression-for-escape-sequence>

,并且只获取其中包含粗体的行。 我不确定应该使用哪些 grep 选项,并且已经尝试过以下操作:

tail -n 1000 -f logfile.log | grep "\033\0133\061\0155"
tail -n 1000 -f logfile.log | grep "\033\01331m"
tail -n 1000 -f logfile.log | grep "\033\[1m"

但它不起作用...是的,logfile.log 的最后 1000 行中有粗体行,测试

echo -e "\033\01331mTest\033\01330m" | grep ...

结果相同...;)

感谢任何帮助!

Hi I have lots of logfiles with ^[[1m (as vim displays it) in them. I want to watch a logfile life via

tail -n 1000 -f logfile.log | grep <expression-for-escape-sequence>

and only get lines that have bold in them.
I am not sure which grep options I should use and have tried the following already:

tail -n 1000 -f logfile.log | grep "\033\0133\061\0155"
tail -n 1000 -f logfile.log | grep "\033\01331m"
tail -n 1000 -f logfile.log | grep "\033\[1m"

It does not work though... And yes there are bold lines in the last 1000 lines of logfile.log, testing with

echo -e "\033\01331mTest\033\01330m" | grep ...

same results... ;)

Appreciate any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

看春风乍起 2024-09-24 23:53:16

使用前面带有美元符号的单引号(如 $'...' 所示),让 shell 将 \033 转义序列转换为 ESC 字符

tail -n 1000 -f logfile.log | grep 

:强>男人重击:

$'string' 形式的单词会被特殊处理。该单词扩展为字符串,并按照 ANSI C 标准的规定替换反斜杠转义字符。

\033\[1m'

:强>男人重击:

$'string' 形式的单词会被特殊处理。该单词扩展为字符串,并按照 ANSI C 标准的规定替换反斜杠转义字符。

Use single quotes with a dollar sign in front—as in $'...'—to have the shell convert the \033 escape sequence into an ESC character:

tail -n 1000 -f logfile.log | grep 

From man bash:

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.

\033\[1m'

From man bash:

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.

别再吹冷风 2024-09-24 23:53:16

这是可行的(在 POSIX shell 中,不一定是 bash):

echo -e "\033\01331mTest\033\01330m" | grep "$(printf "\x1b\\[1m")"

This works (in a POSIX shell, not necessarily bash):

echo -e "\033\01331mTest\033\01330m" | grep "$(printf "\x1b\\[1m")"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文