This is an easy way to remove color characters from log files.
# the sed command:
sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g'
# in actual use:
cat file_with_color.txt | sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g' > file_without_color.txt
This was motivated by trying to search through logfile archives downloaded from GitHub Actions workflow runs. While the color characters look nice, they get in the way of things like ack
and/or grep
. Running the files through this little sed
command removes them.
- There are a lot of Stack Overflow posts about this, this is the one where I got this note from.
- Versions of
sed
differ between Linux and BSD/macOS. This command at least works on macOS Sonoma (14.x).