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

#!/bin/bash

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

./somescript

输出为(在日志文件中):

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"

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

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

我怎么也可以删除这个'@[60G?

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


当前回答

我无法从其他答案中得到满意的结果,但下面的答案对我来说很有用:

somescript | sed -r "s/[[:cntrl:]]\[[0-9]{1,3}m//g"

如果我只删除了控制字符“^[”,它就会留下其余的颜色数据,例如“33m”。加上颜色代码和“m”就成功了。我对s/\x1B//g不工作感到困惑,因为\x1B[31m当然可以与echo一起工作。

其他回答

还有一个专门处理ANSI转义序列的工具:ansifilter。使用默认的——text输出格式去除所有的ANSI转义序列(注意:不仅仅是着色)。

裁判:https://stackoverflow.com/a/6534712

这是一个纯Bash解决方案。

保存为strip-escape-codes.sh,使其可执行,然后执行<command- production - colour -output> | ./strip-escape-codes.sh。

注意,这将删除所有的ANSI转义码/序列。如果你只想去除颜色,用“m”替换[a-zA-Z]。

Bash >= 4.0:

#!/usr/bin/env bash

# Strip ANSI escape codes/sequences [$1: input string, $2: target variable]
function strip_escape_codes() {
    local _input="$1" _i _char _escape=0
    local -n _output="$2"; _output=""
    for (( _i=0; _i < ${#_input}; _i++ )); do
        _char="${_input:_i:1}"
        if (( ${_escape} == 1 )); then
            if [[ "${_char}" == [a-zA-Z] ]]; then
                _escape=0
            fi
            continue
        fi
        if [[ "${_char}" == $'\e' ]]; then
            _escape=1
            continue
        fi
        _output+="${_char}"
    done
}

while read -r line; do
    strip_escape_codes "${line}" line_stripped
    echo "${line_stripped}"
done

Bash < 4.0:

#!/usr/bin/env bash

# Strip ANSI escape codes/sequences [$1: input string, $2: target variable]
function strip_escape_codes() {
    local input="${1//\"/\\\"}" output="" i char escape=0
    for (( i=0; i < ${#input}; ++i )); do         # process all characters of input string
        char="${input:i:1}"                       # get current character from input string
        if (( ${escape} == 1 )); then             # if we're currently within an escape sequence, check if
            if [[ "${char}" == [a-zA-Z] ]]; then  # end is reached, i.e. if current character is a letter
                escape=0                          # end reached, we're no longer within an escape sequence
            fi
            continue                              # skip current character, i.e. do not add to ouput
        fi
        if [[ "${char}" == $'\e' ]]; then         # if current character is '\e', we've reached the start
            escape=1                              # of an escape sequence -> set flag
            continue                              # skip current character, i.e. do not add to ouput
        fi
        output+="${char}"                         # add current character to output
    done
    eval "$2=\"${output}\""                       # assign output to target variable
}

while read -r line; do
    strip_escape_codes "${line}" line_stripped
    echo "${line_stripped}"
done

下面的正则表达式将错过一些ANSI转义码序列,以及3位颜色。regex101.com的示例和修复。

用这个代替:

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

我也有问题,有时,SI字符出现。

例如,输入echo "$(tput setaf 1)foo$(tput sgr0) bar"

这里有一种方法也可以剥离SI字符(shift in) (0x0f)

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

Mac OSX或BSD使用

./somescript | sed $'s,\x1b\\[[0-9;]*[a-zA-Z],,g'

恕我直言,大多数答案都过于努力地限制转义代码中的内容。结果,它们最终会丢失常见的代码,如[38;5;60m(前景色ANSI颜色60来自256色模式)。

它们还需要启用GNU扩展的-r选项。这些都不是必需的;它们只是让正则表达式读起来更好。

下面是一个更简单的答案,它处理256色转义,并在非gnu sed系统上工作:

./somescript | sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g'

这将捕获以[开头,有任意数量的小数和分号,并以字母结尾的任何内容。这应该捕获任何常见的ANSI转义序列。

对于所有可能的ANSI转义序列,这里有一个更大、更通用(但最少测试)的解决方案:

./somescript | sed 's/\x1B[@A-Z\\\]^_]\|\x1B\[[0-9:;<=>?]*[-!"#$%&'"'"'()*+,.\/]*[][\\@A-Z^_`a-z{|}~]//g'

(如果你有@edi9999的SI问题,在后面加上| sed "s/\x0f//g";这适用于任何控制字符,用不需要的字符的十六进制替换0f)