我有一个 perl 程序,它用颜色打印输出。如果我重定向文件中的输出并在 vi 中打开它,我会看到颜色特殊字符。
像这样的东西。
^[[31;43mAnd this is red on_yellow too^[[0m
从输出文件中删除此颜色字符的最佳方法是什么?
Thanks
Update:
我尝试了这个正则表达式。这个对我有用:
cat -v a|head
^[[30;41mThis is black on_red^[[0m
^[[30;41mAnd this is black on_red too^[[0m
^[[30;42mThis is black on_green^[[0m
^[[30;42mAnd this is black on_green too^[[0m
^[[30;43mThis is black on_yellow^[[0m
^[[30;43mAnd this is black on_yellow too^[[0m
^[[30;44mThis is black on_blue^[[0m
^[[30;44mAnd this is black on_blue too^[[0m
^[[30;45mThis is black on_magenta^[[0m
^[[30;45mAnd this is black on_magenta too^[[0m
$ cat -v a|head|perl -lane 's/\^\[\[\d+(;\d+)*m//g; print'
This is black on_red
And this is black on_red too
This is black on_green
And this is black on_green too
This is black on_yellow
And this is black on_yellow too
This is black on_blue
And this is black on_blue too
This is black on_magenta
And this is black on_magenta too
Perl 模块Term::ANSIColor http://perldoc.perl.org/Term/ANSIColor.html提供一个函数,colorstrip()
,就是为了做到这一点。例如,
ls --color | perl -MTerm::ANSIColor=colorstrip -ne 'print colorstrip($_)'
Module Term::ANSIColor
是 Perl 核心的一部分。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)