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

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

问题描述

我有一些产生颜色输出的脚本,我需要删除 ANSI 代码.

I have some script that produces output with colors and I need to remove the ANSI codes.

#!/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 字符放在这里,所以我把 @ 放在它的位置.

I didn't know how to put the ESC character here, so I put @ in its place.

我把脚本改成:

#!/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?

How can I also remove this '@[60G?

也许有一种方法可以完全禁用整个脚本的着色?

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

推荐答案

根据维基百科,您正在使用的 sed 命令中的 [m|K] 专门用于处理 m (颜色命令)和 K(擦除部分行"命令).您的脚本正在尝试将绝对光标位置设置为 60 (^[[60G) 以获取一行中的所有 OK,而您的 sed 行未涵盖该行.

According to Wikipedia, 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,3}(;[0-9]{1,2})?)?[mGK]//g"

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

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