从Linux文件中删除颜色代码 [英] Remove colour code from linux files

查看:136
本文介绍了从Linux文件中删除颜色代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自测试脚本的输出文件(我不能更改),由于编码,该输出在终端中看起来很棒,它以漂亮的颜色显示了输出.

I have an output file from a testing script (which I cannot alter), the output looks great in the terminal thanks to the encoding, which displays the output in nice colours.

但是,当我对文件进行vim处理时,会得到以下信息:

However when I vim the file, I get the following:

^[[1m0024^[[0m, ^[[36munknown.10^[[0m --> ^[[32mUNKNOWN^[[0m

我希望文件包含:

0024, unknown.10 --> UNKNOWN

关于stackover流有两个类似的问题,但是到目前为止,我还没有找到适合我的解决方案.

There are a couple of similar questions on stackover flow, but so far I have not found a solution that works for me.

任何帮助将不胜感激!

非常感谢!

其他信息:

我不想隐藏颜色字符,我想将它们从文件中删除.

I don't want to conceal the colour characters, I would like to remove them from the file.

输出进入证据文件,然后将该文件上推至GIT供团队审核.使用这些颜色代码的GIT UI很难:(

The output goes into an evidence file, and then that file is pushed up to a GIT for the team to review. It is difficult to the GIT UI with those colour codes :(

推荐答案

要删除颜色控制字符,可以使用以下sed命令:

To remove color control character, you may use the following sed command:

sed 's/\x1b\[[^\x1b]*m//g' file

此处所示,颜色代码由< Esc> [FormatCodem .

转义字符为十六进制的 \ x1b (有时标记为 \ e \ 033 ).

The escape character is \x1b in hexadecimal (sometimes noted as \e or \033).

该命令查找序列转义符,后跟方括号 \ x1b \ [,直到字符 m (如果找到)将其删除.

The command looks for the sequence escape followed by square bracket \x1b\[ until the character m, if found it deletes it.

除了转义字符本身 [^ \ x1b] * 之外,这两个字符之间的所有内容均被允许.这允许具有最短的正则表达式.

Everything in between these 2 characters is allowed except the escape character itself [^\x1b]*. This allows to have the shortest regex.

这篇关于从Linux文件中删除颜色代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆