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

时间:2022-05-26 19:36:22

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

有些程序使用ANSI转义序列制作漂亮的进度条和内容。真好。

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?

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

I usually log the output of a script this way:

我通常以这种方式记录脚本的输出:

./script >> /tmp/output.log

1 个解决方案

#1


7  

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.

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

#1


7  

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.

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