从输出清除颜色 [英] Removing colors from output

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

问题描述

您好我有一些脚本,产生颜色和我需要摆脱了这一点。

Hi I have some script that produce colors and I need to get rid of this.

#!/bin/bash

exec > >(tee log)   # redirect the output to a file but keep it on stdout
exec 2>&1

./somescript

的输出是(在日志文件中):

The output is (in log file):

java (pid  12321) is running...@[60G[@[0;32m  OK  @[0;39m]

我不知道怎么把这里的ESC字符,所以我把 @ 在它的地方。

我改剧本为:

#!/bin/bash

exec > >(tee log)   # redirect the output to a file but keep it on stdout
exec 2>&1

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

但现在它给我(在日志文件中):

But now it gives me (in log file):

java (pid  12321) is running...@[60G[  OK  ]

我怎么也删除该 @ 60G

也许有一种为整个脚本完全禁用着色?

Maybe there is a way to completely disable coloring for the entire script?

推荐答案

根据维基百科,该知道所有的东西, [M | K] 在你使用的是专门用来处理 SED 命令 M (颜色命令)和 K (以下简称擦除行的一部分命令)。您的脚本试图绝对光标位置设置为60( ^ [[60G ),以获得在一条线上,你的 SED 行不包括。

According to Wikipedia, which knows all things, the [m|K] in the sed command you're using is specifically designed to handle m (the color command) and K (the "erase part of line" command). Your script is trying to set absolute cursor position to 60 (^[[60G) to get all the OKs in a line, which your sed line doesn't cover.

(正确, [M | K] 也许应该是(M | K) [mK的] ,因为你没有尝试匹配管道符。但是现在这并不重要。)

(Properly, [m|K] should probably be (m|K) or [mK], because you're not trying to match a pipe character. But that's not important right now.)

如果您切换,为最后一场比赛在命令 [MGK] (M | G | K) ,你应该能够赶上那额外的控制序列。

If you switch that final match in your command to [mGK] or (m|G|K), you should be able to catch that extra control sequence.

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

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

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