2

I'm running an expect script that's calling several scripts on a remote machine. Those shell scripts return color output (mostly red and green). The problem is, that those color codes make it into the log_file and STDOUT, which I don't want. I can't modify the remote scripts.

I tried sed, where I pipe the output from expect through sed which removes the color codes. The problem is, that the expect script is run by the Webmin "custom commands" module, which has issues with the piped output. I also tried to call the remote scripts using a sed pipe inside the expect script, but this creates other issues.

Is there a way to remove color codes from STDOUT and log_file directly in expect with something built in?

Hnouss
  • 33
  • 4

1 Answers1

2

The colour code begins with CSI code (ESC+[) followed by numbers which can be separated with ;. Following perl command can be used to remove these sequences:

perl -pe 's/\033\[[\d;]*m//g'

# examples

echo $'\033''[3;31mhi'$'\033''[0m'

echo $'\033''[3;31mhi'$'\033''[0m' | perl -pe 's/\033\[[\d;]*m//g'

EDIT: \033 or \e or \x1b

Other option could be to export TERM variable like:

export TERM=xterm-old
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36