Observando um comentário sobre "Fácil usando o awk", isso seria relativo ao sed.
OP descrevia um recurso não muito bem documentado de logcat
(consulte commit ) que informa logcat
para atribuir cores codificadas a cada linha com base na prioridade da entrada de log.
Existem outras opções, como logcat-color , PID cat e coloredlogcat que permite colorir diferentes campos , por exemplo, aumentando a possibilidade de várias cores por linha.
Aqui está um script awk que reconhece as cores do logcat codificado, mas permite várias cores por linha:
#!/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 = "";
}
/3/ {
done = "";
while ( $0 ~ /3\[[;0-9]*m/ ) {
mark = match($0, /3\[[;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, /^3\[[;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;
}
Com o exemplo original:
^[[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
você receberia a saída solicitada:
[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]
mas alterando a entrada para
^[[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
fornece este resultado:
[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]