简体   繁体   中英

perl: capturing STDOUT- logfile ends up binary

I wrote a script that feeds data into an exe program called ctffind. The exe program outputs a bunch of data to the screen which I'm trying to capture in a logfile.

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". In my next step I do grep "final values" logfile it thinks logfile is a binary file and doesn't work instead it returns:

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?

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 .

  2. One can use strings logfile logfile2 to make a usable version of the logfile.

An Ascii NUL (\\000) in a file's first block is enough for Perl to call the file a "binary" one.

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:

  • You can pipe it through cat -v which will turn all nonprintables into ASCII representations (eg NUL becomes ^@ ).
  • There may be specific utilities for stripping ANSI or VT100 sequences out of text--try asking superuser.
  • 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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