How to remove the decorate colors characters in bash output?

若如初见. 提交于 2019-12-30 10:07:08

问题


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.

I tried with tr -d "^[[22m" and with sed 's/[\^[[22m]//g', but only is removed the number, not the special character ^[

Thanks.


回答1:


You have multiple options:

  • 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
  • and as -no-ansi as pointed out by Jens in other answer

EDIT

The solution from commandlinefu does the job pretty well:

sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"

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.




回答2:


I found this in the manual about the use of ANSI escape codes:

-no-ansi
    Do not use ANSI escape codes.

So you should add this option when starting the program.



来源:https://stackoverflow.com/questions/32166976/how-to-remove-the-decorate-colors-characters-in-bash-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!