从字符串中删除前导和尾随ansi / tput代码

问题描述 投票:1回答:2

这里的应用程序是“清理”字符串以包含在日志文件中。为了论证,我们假设1)在运行时对字符串着色是正确的; 2)我需要屏幕上的前导和尾随空格,但从日志中删除了多余的空格。

这里的具体应用是发布到日志文件中。并非所有线都会着色,并且并非所有线都具有前导/尾随空格。

鉴于此,我想

  1. 删除所有设置颜色和重置的代码。其原因很明显
  2. 删除前导和尾随空格

当您搜索(在任何地方)如何在bash中删除颜色代码时,您可以找到many different ways来完成它。到目前为止我发现的是,似乎没有人能解决拖尾重置问题。 $(tput sgr0)。在示例中,我已经看到这是无关紧要的,但是我对剥离前导/尾随空格的额外要求使其变得复杂/使其成为一项要求。

这是我的示例脚本,它演示了这个问题:

#!/bin/bash

# Create a string with color, leading spaces, trailing spaces, and a reset
REPLY="$(tput setaf 2)       This is green        $(tput sgr0)"
echo "Colored output:  $REPLY"
# Remove initial color code
REPLY="$(echo "$REPLY" | sed 's,\x1B\[[0-9;]*[a-zA-Z],,g')"
echo "De-colorized output:  $REPLY"
# Remove leading and trailing spaces if present
REPLY="$(printf "%s" "${REPLY#"${REPLY%%[![:space:]]*}"}" | sed -n -e 'l')"
echo "Leading spaces removed:  $REPLY"
REPLY="$(printf "%s" "${REPLY%"${REPLY##*[![:space:]]}"}" | sed -n -e 'l')"
echo "Trailing spaces removed:  $REPLY"

输出是(无法弄清楚如何在这里着色文本,假设第一行是绿色,后续行不是):

screen cap

我愿意看到我的方式的错误,但经过大约三个小时尝试不同的事情,我很确定我的google-fu让我失望了。

谢谢你的帮助。

bash logging sed colors ansi
2个回答
0
投票

我愿意看到我的方式的错误,......

主要错误只是sed命令只删除了Esc [...控制序列,而不是Esc(B序列也是sgr0的一部分。如果你将它改为

… | sed 's,\x1B[[(][0-9;]*[a-zA-Z],,g'

次要错误是sed -n -e 'l'命令在行的末尾添加了一个文字$符号,因此前一个尾随空格不再尾随,因此不会被删除。


0
投票

这对我有用:

$ REPLY="$(tput setaf 2)       This is green        $(tput sgr0)"
$ echo -n $REPLY | od -vAn -tcx1
 033   [   3   2   m                               T   h   i   s
  1b  5b  33  32  6d  20  20  20  20  20  20  20  54  68  69  73
       i   s       g   r   e   e   n                            
  20  69  73  20  67  72  65  65  6e  20  20  20  20  20  20  20
     033   [   m 017
  20  1b  5b  6d  0f
$ REPLY=$(echo $REPLY | sed -r 's,\x1B[\[\(][0-9;]*[a-zA-Z]\s*(.*)\x1B[\[\(].*,\1,g' | sed 's/\s*$//')
$ echo -n $REPLY | od -vAn -tcx1
   T   h   i   s       i   s       g   r   e   e   n
  54  68  69  73  20  69  73  20  67  72  65  65  6e

显然sed does not support非贪婪的正则表达式,这将取消第二个正则表达式。

编辑:这个应该适用于您的输入:

$ REPLY="$(tput setaf 2)       This is green        "$'\x1B'"(B$(tput sgr0)"
$ echo -n $REPLY | od -vAn -tcx1
 033   [   3   2   m                               T   h   i   s
  1b  5b  33  32  6d  20  20  20  20  20  20  20  54  68  69  73
       i   s       g   r   e   e   n                            
  20  69  73  20  67  72  65  65  6e  20  20  20  20  20  20  20
     033   (   B 033   [   m 017
  20  1b  28  42  1b  5b  6d  0f
$ REPLY=$(echo "$REPLY" | sed -r -e 's,\x1B[\[\(][0-9;]*[a-zA-Z]\s*([^\x1B]+)\s+\x1B.*,\1,g' -e 's,\s*$,,')
$ echo -n $REPLY | od -vAn -tcx1
   T   h   i   s       i   s       g   r   e   e   n
  54  68  69  73  20  69  73  20  67  72  65  65  6e

与bash替换相比,我发现sed不那么神秘(或者说正则表达式可能不那么神秘)。但那只是我:)

© www.soinside.com 2019 - 2024. All rights reserved.