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

查看:136
本文介绍了如何从变量中剥离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 "\033")'\[33m%%g')

显然,这仅适用于该一种特定颜色(黄色),而用于删除任何转义序列(例如其他颜色,背景颜色,光标位置等)的正则表达式将<一个href ="https://unix.stackexchange.com/questions/55546/removing-color-codes-from-output">稍微复杂些.

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天全站免登陆