The idea of a sed script is okay (and there are several scripts available for this purpose), but the script suggested could be improved:
- this chunk
s/\x1b_[^\x1b]*\x1b[\]//g might be intended to filter out application mode commands. However, the reader is unlikely to find these used, as noted in the xterm documentation:
None. xterm implements no APC functions; Pt is ignored. Pt need not be printable characters.
- if the underscore were changed to a right-square-bracket
], then that would match some operating system controls. Again, however the main use of those in terminals uses the variant ending with
BEL) for xterm-style titles (often used in bash/zsh prompt-strings).
Given those considerations, a better first chunk might be a non-greedy match for either escape\ or BEL. But
sed makes greedy matches. Rather than get complicated, just
The other chunk has a problem as well. It is too greedy from the start:
because it assumes that after getting rid of application mode (or operating system) commands, the only remaining escape sequences in the shell's output are going to be those that change video modes, i.e., SGR (ending with
m). With a slight change, the expression would filter out only SGR sequences, and not get carried away and remove everything beginning with a escape[, e.g.,
Filtering out color escape sequences can be considered a special case in filtering terminal output to plain text, as answered in Can I programmatically “burn in” ANSI control codes to a file using unix utils? a week before this question was asked.