存储脚本输出时删除 ANSI 代码

发布于 2024-11-14 18:27:03 字数 207 浏览 6 评论 0原文

有些程序使用 ANSI 转义序列制作漂亮的进度条和东西。那很好。

但不好的是,如果我将这种程序的输出放入一个文件中,然后尝试查看它,它就会充满奇怪的转义序列。

有没有办法在记录时去掉所有 ANSI 代码?

我通常这样记录脚本的输出:

./script >> /tmp/output.log

Some programs makes beautiful progressbars and stuff using ANSI escape sequences. That's nice.

What's not nice though is that if i put the output of that kind of program into a file and then try to view it it's filled with strange escape sequences.

Is there a way to strip away all the ANSI codes while logging?

I usually log the output of a script this way:

./script >> /tmp/output.log

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

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

发布评论

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

评论(2

英雄似剑 2024-11-21 18:27:03

尝试:

$ TERM=dumb ./script >> /tmp/output.log

如果这不起作用,那是因为 ANSI 代码已被硬编码到脚本中,因此没有简单的方法可以删除它们。如果确实如此,那是因为它正在做正确的事情,将诸如漂亮输出之类的事情委托给 libncurses 或类似的东西,这样当您更改 TERM 变量时,库就不再发送那些代码。

Try:

$ TERM=dumb ./script >> /tmp/output.log

If that doesn't work, it's because the ANSI codes have been hard-coded into the script, so there is no easy way to remove them. If it does, it's because it's doing the right thing, delegating things like pretty output to libncurses or similar, so that when you change the TERM variable, the library no longer sends those codes.

乜一 2024-11-21 18:27:03

使用 ansifilter

$ ./script | ansifilter >> /tmp/output.log

use ansifilter:

$ ./script | ansifilter >> /tmp/output.log
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文