简体   繁体   中英

Expect: Remove color codes from output and log file

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM