linuxmint/xed

ANSI colorcodes in bash with tee command results in a weird formatted logfile

Opened this issue · 1 comments

Hi there,

When I'm using ANSI colorcodes in a bash-script and and redirect the stdout to a textfile, I'll get no colors, only weird code.
For example do in terminal:

#!/bin/bash
#
# ANSI Colorcodes 
# use: $COLOR text-to-colorize $NORMAL
NORMAL='\033[0m'
WHITE='\033[0;37m'
BLACK='\033[0;30m'
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'

echo -e $GREEN"Show formatted datetime:\t$(date +'%H:%M:%S @ %d.%m.%Y')\n"$NORMAL | tee ~/.test.log

results in:

xed

Info about OS / xed version:

$ xed --version
xed - Version 3.2.8
$ neofetch
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄                nightwing@Jarvis 
▀▀█  █        █                ---------------- 
  █  █  █  █  █                OS: Linux Mint 21.1 x86_64 
  █  █  █  █  █                Host: HP 250 G8 Notebook PC 
  █  █  █  █  █                Kernel: 6.1.0-1007-oem 
  █  ▀▀▀▀▀▀▀  █                Uptime: 16 mins 
  ▀▀▀▀▀▀▀▀▀▀▀▀▀                Packages: 2738 (dpkg) 
                               Shell: bash 5.1.16 
                               Resolution: 1920x1080 
                               DE: Cinnamon 5.6.8 
                               WM: Mutter (Muffin) 
                               WM Theme: Mint-Y-Dark-Aqua (Mint-Y) 
                               Theme: Mint-Y-Dark-Aqua [GTK2/3] 
                               Icons: Mint-Y-Dark-Aqua [GTK2/3] 
                               Terminal: gnome-terminal 
                               CPU: 11th Gen Intel i5-1135G7 (8) @ 4.200GHz 
                               GPU: Intel TigerLake-LP GT2 [Iris Xe Graphics] 
                               Memory: 2019MiB / 15741MiB 

The question now logically is: How can I avoid this weird output / correct it as a syntax?

Thanks in advance for answers,

ToM

Solved by research: Using the way via tmpfile, at the end of the script this entry:

cat ~/.tmp.log | sed -r "s/\x1B\[([0-9]{1,3}(;[0-9]{1,2})?)?[mGK]//g" > ~/.update.log
rm ~/.tmp.log

Can this be build with an internal highlight solution?

ToM