简体   繁体   English

perl:捕获STDOUT-日志文件最终以二进制形式出现

[英]perl: capturing STDOUT- logfile ends up binary

I wrote a script that feeds data into an exe program called ctffind. 我写了一个脚本,将数据输入到一个名为ctffind的exe程序中。 The exe program outputs a bunch of data to the screen which I'm trying to capture in a logfile. exe程序向我试图在日志文件中捕获的屏幕上输出一堆数据。

Here's what I'm using right now 这是我现在正在使用的

my $logout = logfile 
open STDOUT, '>>', "$logout";
open my $PIPE1, '|-', '/opt/ctf/ctffind3_mp.exe' or die $!;

It works outputting everything that appeared on the screen into logfile. 它可以将屏幕上显示的所有内容输出到日志文件中。

If I more $logfile the file displays as expected it's about 5000 lines long and has about 50 lines that contain the string "final values". 如果我more $logfile该文件将按预期显示,它的长度约为5000行,并且包含字符串“最终值”的行数约为50。 In my next step I do grep "final values" logfile it thinks logfile is a binary file and doesn't work instead it returns: 在下一步中,我执行grep "final values" logfile它认为日志文件是二进制文件,因此不起作用,而是返回:

Binary file logfile matches

How do set it so that logfile is properly encoded? 如何设置它以便对日志文件进行正确编码? ctffind.exe is also generating binary files while it runs, is this part of the problem? ctffind.exe在运行时也会生成二进制文件,这是问题的一部分吗?

I have found two different ways to resolve the problem, thanks to commenters' suggestions: 由于评论者的建议,我发现了两种解决问题的方法:

  1. One can change the grep logfile to grep -a logfile . 可以将grep logfile更改为grep -a logfile

  2. One can use strings logfile logfile2 to make a usable version of the logfile. 可以使用strings logfile logfile2来创建strings logfile logfile2的可用版本。

An Ascii NUL (\\000) in a file's first block is enough for Perl to call the file a "binary" one. 文件的第一个块中的Ascii NUL(\\ 000)足以使Perl将该文件称为“二进制”文件。

echo "hello world\000" > myfile
perl -E '$f=shift;open $fh,"<",$f or die;say -e $f && -B $f ? "binary":"text"' myfile

It's possible that ctffind3_mp.exe has code in it to do screen formatting such as changing colors, bolding, clearing the screen, etc. There are a few ways you could eliminate those: ctffind3_mp.exe可能包含ctffind3_mp.exe代码来进行屏幕格式化,例如更改颜色,加粗字体,清除屏幕等。您可以通过以下几种方法消除这些内容:

  • You can pipe it through cat -v which will turn all nonprintables into ASCII representations (eg NUL becomes ^@ ). 您可以通过cat -v管道cat -v它,它将所有不可打印的内容转换为ASCII表示形式(例如NUL变为^@ )。
  • There may be specific utilities for stripping ANSI or VT100 sequences out of text--try asking superuser. 可能有一些特定的实用程序可用于从文本中剥离ANSI或VT100序列-尝试询问超级用户。
  • You could try setting $TERM (or in perl $ENV{'TERM'} ) to unknown in the hopes that the program no longer emits any special sequences for color, bold, etc. 您可以尝试将$TERM (或在perl $ENV{'TERM'} )设置为unknown ,以希望程序不再发出任何特殊的颜色,粗体等顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM