问题描述
控制台程序 (translate-shell) 有一个带有颜色的输出,并为此使用特殊的装饰字符:^[[22m, ^[[24m, ^[[1m... 等等.
A console program (translate-shell) has an output with colors and uses special decorate characters for this: ^[[22m, ^[[24m, ^[[1m... and so on.
我想删除它们以获得纯文本.
I'd like to remove them to get a plain text.
我尝试使用 tr -d "^[[22m" 和 sed 's/[^[[22m]//g',但只删除了数字,而不是特殊字符 ^[
I tried with tr -d "^[[22m" and with sed 's/[^[[22m]//g', but only is removed the number, not the special character ^[
谢谢.
推荐答案
您有多种选择:
- https://unix.stackexchange.com/questions/14684/removing-control-chars-including-console-codes-colours-from-script-output
- http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed
- 以及 Jens 在其他答案中指出的
-no-ansi
编辑
来自commandlinefu的解决方案 做得很好:
sed -r "s/x1B[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
来自 unix.stackexchange 的解决方案 可能更好,但要长得多,因此您需要创建一个单独的脚本文件,因为它太长了,而不仅仅是做一个 shell 单行.
The solution from unix.stackexchange might be better but is much longer and so you would want to create a separate script file because it is so long instead of just doing a shell one-liner.
这篇关于如何删除bash输出中的装饰颜色字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!