색상으로 출력을 생성하는 명령이 있으며 색상 코드를 제거하여 파일로 파이프하고 싶습니다. 색상 코드를 제거한다는 점을 제외하고 cat
처럼 작동하는 명령이 있습니까? 나는 이런 식으로 할 계획이다 :
$ command-that-produces-colored-output | stripcolorcodes > outfile
이를위한 유틸리티가 있다고 생각하지만 찾을 수 없었습니다. 그러나이 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;
}
only 색상 코드를 제거하고 다른 ANSI 코드 (커서 이동과 같은)를 그대로 두려면
s/\e\[[\d;]*m//g;
위에서 사용한 대체 대신 모든 ANSI 이스케이프 코드를 제거합니다.
sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
또는
설치 colorama 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
.
Term :: ANSIColor 모듈을 설치할 수 있으면이 Perl 스크립트가 작동합니다.
#!/usr/bin/env Perl
use Term::ANSIColor qw(colorstrip);
print colorstrip $_ while <>;
시스템이 NodeJS 에 액세스 할 수 있으면 다음 Node 패키지, strip-ansi-cli
.
$ npm install -g strip-ansi-cli
그런 다음 명령을 다음과 같이 실행할 수 있습니다.
$ command-that-produces-colored-output | strip-ansi > outfile
이 sed 명령은 나를 위해 그것을했습니다 :
sed -r "s/\\^\\[(\\[[^@-~]+[@-~]|[[email protected]_]|%@)//g"
예:
$ command-that-produces-colored-output | sed -r "s/\\^\\[(\\[[^@-~]+[@-~]|[[email protected]_]|%@)//g" > outfile
이를 위해 ac 를 사용할 수 있습니다. 위에 나열된 것보다 빠릅니다 (Perl
및 sed
는 괜찮음). 예를 들면 다음과 같습니다.
curl -s wttr.in/LA | ac -s
면책 조항 : 컬러 필터링 기능은 저에 의해 작성되었습니다.
$ command-that-produces-colored-output | ansifilter
... 그리고 필요한 경우 (dnf, ...) install ansifilter