Bash - 将转义字符彩色文本转换为 BBCode

Bash - 将转义字符彩色文本转换为 BBCode

我想将 a 转换terminal escape-character colored text fileBBCode彩色文本。为此,我创建了一个 Android 日志文件,其中包含logcat -Cd > /sdcard/logcat.txt.该-C开关添加了颜色转义字符。输出如下所示:

[0m[38;5;231mV/Zygote  ( 4666): Switching descriptor 55 to /dev/null
[0m[38;5;231mV/Zygote  ( 4666): Switching descriptor 9 to /dev/null
[0m[38;5;40mI/ggheart ( 1111): onStop
[0m[38;5;40mI/Test    ( 1111): onStop
[0m[38;5;75mD/ActivityThread( 4666): handleBindApplication:com.mxtech.videoplayer.ad
[0m[38;5;75mD/ActivityThread( 4666): setTargetHeapUtilization:0.75
[0m[38;5;75mD/ActivityThread( 4666): setTargetHeapMinFree:2097152

要将颜色代码转换为BBCode我编写的sed脚本:

#!/bin/bash
#Use 'logcat -Cd > /sdcard/logcat.txt' as input file

sed '/\x1b/ {
    s/\x1b\[0m\x1b\[38;5;40m/\[COLOR="Green"\]/
    s/\x1b\[0m\x1b\[38;5;196m/\[COLOR="Red"\]/
    s/\x1b\[0m\x1b\[38;5;75m/\[COLOR="Blue"\]/
    s/\x1b\[0m\x1b\[38;5;166m/\[COLOR="Sienna"\]/
    s/\x1b\[0m\x1b\[38;5;231m/\[COLOR="DarkSlateGray"\]/
    s/\x1b\[38;5;40m/\[COLOR="Green"\]/
    s/\x1b\[38;5;196m/\[COLOR="Red"\]/
    s/\x1b\[38;5;75m/\[COLOR="Blue"\]/
    s/\x1b\[38;5;166m/\[COLOR="Sienna"\]/
    s/\x1b\[38;5;231m/\[COLOR="DarkSlateGray"\]/
    s/\x1b\[0m/\[COLOR="Black"\]/
    s/$/\[\/COLOR\]/
    }' <logcat.txt >logcat2.txt

处理后的输出文本如下所示:

[COLOR="DarkSlateGray"]V/Zygote  ( 4666): Switching descriptor 55 to /dev/null[/COLOR]
[COLOR="DarkSlateGray"]V/Zygote  ( 4666): Switching descriptor 9 to /dev/null[/COLOR]
[COLOR="Green"]I/ggheart ( 1111): onStop[/COLOR]
[COLOR="Green"]I/Test    ( 1111): onStop[/COLOR]
[COLOR="Blue"]D/ActivityThread( 4666): handleBindApplication:com.mxtech.videoplayer.ad[/COLOR]
[COLOR="Blue"]D/ActivityThread( 4666): setTargetHeapUtilization:0.75[/COLOR]
[COLOR="Blue"]D/ActivityThread( 4666): setTargetHeapMinFree:2097152[/COLOR]

COLOR这在语法上是正确的,并且在相应的论坛板中可以正常工作,但它没有优化,并且由于标签不跨越多行而浪费了太多字符(在大多数板中是有限的) 。

它应该看起来像这样,其中相同颜色的线不会关闭/重新打开相同的COLOR标签:

[COLOR="DarkSlateGray"]V/Zygote  ( 4666): Switching descriptor 55 to /dev/null
V/Zygote  ( 4666): Switching descriptor 9 to /dev/null[/COLOR]
[COLOR="Green"]I/ggheart ( 1111): onStop
I/Test    ( 1111): onStop[/COLOR]
[COLOR="Blue"]D/ActivityThread( 4666): handleBindApplication:com.mxtech.videoplayer.ad
D/ActivityThread( 4666): setTargetHeapUtilization:0.75
D/ActivityThread( 4666): setTargetHeapMinFree:2097152[/COLOR]

知道如何实现这一目标吗?基于行的文本处理器是否有可能?只要它运行在 shell/bash/cygwin 上,它就不是强制性的。

答案1

注意到有关“Easy using awk”的评论,这与 sed 相关。

OP 描述了一个没有很好记录的功能logcat(参见犯罪)它告诉logcat根据日志条目为每一行分配硬编码颜色优先事项

还有其他选择,例如logcat-颜色,PID猫彩色日志猫让你的颜色不同领域例如,提高每行多种颜色的可能性。

这是一个 awk 脚本,它可以识别硬编码的 logcat 颜色,但允许每行使用多种颜色:

#!/usr/bin/awk -f
BEGIN {
        colors["0"]        = "Black";
        colors["38;5;40"]  = "Green";
        colors["38;5;196"] = "Red";
        colors["38;5;75"]  = "Blue";
        colors["38;5;166"] = "Sienna";
        colors["38;5;231"] = "DarkSlateGray";
        color = "";
        last = "";
        this = "";
        save = "";
}
/\033/ {
        done = "";
        while ( $0 ~ /\033\[[;0-9]*m/ ) {
                mark = match($0, /\033\[[;0-9]*m/ );
                if ( mark > 1 ) { done = done substr($0, 1, mark - 1); }
                item = substr($0, RSTART + 2, RLENGTH - 3);
                $0 = substr($0, RSTART + RLENGTH);
                if ( match($0, /^\033\[[;0-9]*m/ ) > 0 ) continue;
                color = colors[item];
                if ( done == "" ) this = color;
                if ( item == "0" ) color = "";
                if ( color == "" ) {
                        if ( $0 != "" ) last = color;
                        $0 = "[/COLOR]" $0;
                } else if (color != last) {
                        $0 = "[COLOR=\"" color "\"]" $0;
                        last = color;
                }
        }
        $0 = done $0;
        if ( last != "" ) $0 = $0 "[/COLOR]";
}
{
        if ( NR > 1 ) {
                if ( this == last) sub("\[/COLOR\]$", "", save);
                print save;
        }
        save = $0;
}
END {
        if ( NR > 0 ) print save;
}

以原来的例子:

^[[0m^[[38;5;231mV/Zygote  ( 4666): Switching descriptor 55 to /dev/null
^[[0m^[[38;5;231mV/Zygote  ( 4666): Switching descriptor 9 to /dev/null
^[[0m^[[38;5;40mI/ggheart ( 1111): onStop
^[[0m^[[38;5;40mI/Test    ( 1111): onStop
^[[0m^[[38;5;75mD/ActivityThread( 4666): handleBindApplication:com.mxtech.videoplayer.ad
^[[0m^[[38;5;75mD/ActivityThread( 4666): setTargetHeapUtilization:0.75
^[[0m^[[38;5;75mD/ActivityThread( 4666): setTargetHeapMinFree:2097152

你会得到所请求的输出:

[COLOR="DarkSlateGray"]V/Zygote  ( 4666): Switching descriptor 55 to /dev/null
V/Zygote  ( 4666): Switching descriptor 9 to /dev/null
[COLOR="Green"]I/ggheart ( 1111): onStop
I/Test    ( 1111): onStop
[COLOR="Blue"]D/ActivityThread( 4666): handleBindApplication:com.mxtech.videoplayer.ad
D/ActivityThread( 4666): setTargetHeapUtilization:0.75
D/ActivityThread( 4666): setTargetHeapMinFree:2097152[/COLOR]

但将输入更改为

^[[0m^[[38;5;231mV/Zygote  ( 4666): Switching descriptor 55 to /dev/null
^[[0m^[[38;5;231mV/Zygote  ( 4666): Switching descriptor 9 to /dev/null
^[[0m^[[38;5;40mI/ggheart ( ^[[38;5;75mI/ggheart 1111^[[0m): onStop
^[[0m^[[38;5;40mI/Test    ( 1111): onStop
^[[0m^[[38;5;75mD/ActivityThread( 4666): handleBindApplication:com.mxtech.videoplayer.ad
^[[0m^[[38;5;75mD/ActivityThread( 4666): setTargetHeapUtilization:0.75
^[[0m^[[38;5;75mD/ActivityThread( 4666): setTargetHeapMinFree:2097152

给出这个结果:

[COLOR="DarkSlateGray"]V/Zygote  ( 4666): Switching descriptor 55 to /dev/null
V/Zygote  ( 4666): Switching descriptor 9 to /dev/null[/COLOR]
[COLOR="Green"]I/ggheart ( [COLOR="Blue"]I/ggheart 1111[/COLOR]): onStop
[COLOR="Green"]I/Test    ( 1111): onStop
[COLOR="Blue"]D/ActivityThread( 4666): handleBindApplication:com.mxtech.videoplayer.ad
D/ActivityThread( 4666): setTargetHeapUtilization:0.75
D/ActivityThread( 4666): setTargetHeapMinFree:2097152[/COLOR]

答案2

听起来更容易perl

perl -0777 -pe '
  BEGIN{
    %c = (
      "38;5;40" => "Green",
      "38;5;196" => "Red",
      "38;5;75" => "Blue",
      "38;5;166" => "Sienna",
      "38;5;231" => "DarkSlateGray",
      "38;5;40" => "Green",
      "38;5;196" => "Red",
      "38;5;75" => "Blue",
      "38;5;166" => "Sienna",
      "38;5;231" => "DarkSlateGray",
      "0" => "Black");
    $esc = qr{\e\[([\d;]*)m};
  }

  s{$esc(.*?)(?=$|$esc)}{
    $ret = $2;
    if ($2 ne "" && $1 ne $last) {
      $ret = (defined($last) && "[/COLOR]") . "[COLOR=\"$c{$1}\"]$2";
      $last = $1
    }
    $ret
  }gse;

  s{$}{[/COLOR]} if $last'

(这里采用惰性方法并将整个文件加载到内存中。

相关内容