STDIN을 STDOUT으로 전달하고 색상 코드를 제거하는 프로그램이 있습니까?

STDIN을 STDOUT으로 전달하고 색상 코드를 제거하는 프로그램이 있습니까?

컬러 출력을 생성하는 명령이 있는데 이를 파일로 파이프하여 컬러 코드를 제거하고 싶습니다. cat색상 코드를 제거한다는 점을 제외하고 유사하게 작동하는 명령이 있습니까 ? 나는 다음과 같은 일을 할 계획입니다 :

$ command-that-produces-colored-output | stripcolorcodes > outfile

답변1

이를 달성하기 위한 유틸리티가 있다고 생각할 수도 있지만 찾을 수 없습니다. 그러나 이 Perl 문은 트릭을 수행해야 합니다.

perl -pe 's/\e\[?.*?[\@-~]//g'

예:

$ command-that-produces-colored-output | perl -pe 's/\e\[?.*?[\@-~]//g' > outfile

또는 스크립트를 원하는 경우 다음과 같이 저장할 수 있습니다 stripcolorcodes.

#! /usr/bin/perl

use strict;
use warnings;

while (<>) {
  s/\e\[?.*?[\@-~]//g; # Strip ANSI escape codes
  print;
}

네가 그걸 벗고 싶다면오직색상 코드를 저장하고 다른 ANSI 코드(예: 커서 이동)를 유지하려면 다음을 사용하세요.

s/\e\[[\d;]*m//g;

위에서 사용한 대체 코드 대신(모든 ANSI 이스케이프 코드 제거)

답변2

GNU sed를 사용하여 색상 코드(특수 문자) 제거

sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"

또는

Python에서 ANSI 이스케이프 시퀀스 제거

설치하다셀라마Python 패키지( pip install colorama).입력 stripcolorcodes:

#!/usr/bin/env python
import colorama, fileinput, sys;
colorama.init(strip=True);

for line in fileinput.input():
    sys.stdout.write(line)

달리기 chmod +x stripcolorcodes.

답변3

설치할 수 있는 경우용어::ANSI색상모듈에서 다음 Perl 스크립트가 작동합니다.

#!/usr/bin/env perl
use Term::ANSIColor qw(colorstrip);
print colorstrip $_ while <>;

답변4

$ command-that-produces-colored-output | ansifilter

...필요하다면,(dnf, ...) install ansifilter

관련 정보