如何从变量中去除 ANSI 转义序列? [英] How to strip ANSI escape sequences from a variable?

查看:20
本文介绍了如何从变量中去除 ANSI 转义序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

奇怪的问题.当我在 Bash 中设置一个变量以显示为某种颜色时,我不知道如何重置它.下面是一个例子:

Weird question. When I set a variable in Bash to display as a certain color, I don't know how to reset it. Here is an example:

首先定义颜色代码:

YELLOW=$(tput setaf 3)
RESET=$(tput sgr0)    

现在设置错误消息变量和它的颜色部分.

Now set the error message variable and color part of it.

ERROR="File not found: "$YELLOW"Length.db$RESET"

这将变量 ERROR 设置为将从最终显示在终端上的函数返回的错误消息.除了文件名之外,错误都是白色的.文件名以黄色突出显示给用户.

This sets the variable ERROR as the error message to be returned from a function that will eventually be displayed on the terminal. The error will be all white with the exception of the file name. The file name is highlighted yellow for the user.

这很好用,除非使用 rsyslog 记录.当错误消息被记录时,它会显示如下:

This works great except when logging with rsyslog. When the error message gets logged, it comes out something like this:

找不到文件:#033[33mLength.db#033(B#033[m

File not found: #033[33mLength.db#033(B#033[m

这显然使日志文件很难阅读.起初我想我可以在输出到终端后但在登录之前立即使用 sed 处理错误消息,但没有什么可以搜索和替换的.即,我想我可以使用 sed 来做类似的事情:

This obviously makes log files very difficult to read. At first I figured I could process using sed the error message immediately after outputting to the terminal but before logging, but there is nothing to search for and replace. ie, I thought I could use sed to do something similar to this:

ERROR=$(echo "$ERROR" | sed -r 's%#033[33m%%')

但是当您回显变量时这些字符不存在(这是有道理的,因为您没有在终端上看到它).所以我卡住了.我不知道如何在设置后重置变量的颜色.我还尝试使用 $RESET 以某种方式逆转该过程,但也许我的语法有误或什么的.

But those characters are not present when you echo the variable (which makes sense since you dont see it on the terminal). So im stuck. I dont know how to reset the color of the variable after setting it. I also tried to reverse the process somehow using $RESET but maybe my syntax is wrong or something.

推荐答案

您几乎拥有了它.试试这个:

You almost had it. Try this instead:

ERROR=$(echo "$ERROR" | sed 's%o033[33m%%g')

但是请注意,在 sed 中使用 oNNN 转义序列是 GNU 扩展,因此不符合 POSIX.如果这是一个问题,您应该可以执行以下操作:

Note, however, that the use of the oNNN escape sequence in sed is a GNU extension, and thus not POSIX compliant. If that is an issue, you should be able to do something more like:

ERROR=$(echo "$ERROR" | sed 's%'$(echo -en "33")'[33m%%g')

显然,这仅适用于这种特定颜色(黄色),而用于删除任何转义序列(例如其他颜色、背景颜色、光标定位等)的正则表达式将是 有点复杂.

Obviously, this will only work for this one specific color (yellow), and a regex to remove any escape sequence (such as other colors, background colors, cursor positioning, etc) would be somewhat more complicated.

还要注意 -r 不是必需的,因为这里没有使用扩展的正则表达式语法.我猜你已经知道了,而且你只是出于习惯包含了 -r,但为了清楚起见,我还是提到了它.

Also note that the -r is not required, since nothing here is using the extended regular expression syntax. I'm guessing you already know that, and that you just included the -r out of habit, but I mention it anyway just for the sake of clarity.

这篇关于如何从变量中去除 ANSI 转义序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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