Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想在终端中运行一个命令并捕获输出,所以我正在使用
my $output = `command`;
问题是该命令具有语法突出显示,所以我稍后会打印我失去语法突出显示,而是得到诸如
print $output;
结果
←[31merror←[39m ←[
我怎样才能在没有语法高亮的情况下获取命令,或者以某种方式让语法高亮显示在打印上。
试试这个从 shell 输出中删除 ANSI 颜色转义:
my $output = `command`; my $output =~ s/\e\[[\d;]*m//g; print "$output","\n";
如果要删除所有 ANSI 转义序列,请将正则表达式替换为:
s/\e\[?.*?[\@-~]//g