gpt4 book ai didi

perl - 在 Unix 中删除 ANSI 颜色转义的最佳方法

转载 作者:行者123 更新时间:2023-12-03 18:18:16 27 4
gpt4 key购买 nike

我有一个 perl 程序,它的打印输出带有颜色。如果我重新编辑文件中的输出并在 vi 中打开它,我会看到颜色特殊字符。
像这样的东西。

^[[31;43mAnd this is red on_yellow too^[[0m

从输出文件中删除此颜色字符的最佳方法是什么?

谢谢

更新:

我尝试了正则表达式。这个对我有用:
 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 提供一个函数,colorstrip() , 就这样做。例如,

ls --color | perl -MTerm::ANSIColor=colorstrip -ne 'print colorstrip($_)'

模块 Term::ANSIColor是 Perl 核心的一部分。

关于perl - 在 Unix 中删除 ANSI 颜色转义的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7394889/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com