Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

On liner STDIN

Status
Not open for further replies.

malpa

Technical User
Feb 8, 2004
122
CO
Hi

Im decoding a binary file, this file has this structure

HEx
bc 08 00 50 00 31 48 27 20 83 ff ff ff ff 03 87 40 55 90 20 11 07 31 05 04 29 00 00 02 75 00 00 00 00 01 21 00 16
bc 08 00 50 00 31 34 56 93 90 ff ff ff ff 03 87 40 55 90 20 11 07 31 05 09 00 00 00 00 30 00 00 00 00 01 21 00 16

I decode the file and count the registers using this programm

perl myprogramm <file> | wc -l

18916 registers.


#!/usr/bin/perl
$file1 = $ARGV[0];
open(FILE1,"<$file1") or die "Input file: Cannot open file\n\n";
$[=1;
seek FILE1,0 ,0;
my $e=0;
while ( sysread (FILE1, my $rec,1) ) {
my @reg3 = unpack ("H2",$rec);
if ( $reg3[1] eq "bc" ){
my $ptr = tell ( FILE1 );
#print $ptr."\n";
seek FILE1,-1,1;
sysread (FILE1, my $rec,38);
@reg3 = unpack ("H2 H2 H2 H2 H2 H2H2H2H2H2 H2H2H2H2H2H2H2H2H2 H2 H2H2H2H2H2H2 H2H2H2H2 H2H2H2H2 H2H2 H2H2",$rec);
for ( $i=1; $i<=38;$i++){
print $reg3[$i]." ";
}
print "\n";
}
}
close(FILE1);


but If a try to run the same programm in one line, only print half the registers.

cat <file> | perl myprogram2.pl | wc -l

9640 registers.

myprogramm2.pl

#!/usr/bin/perl
$[=1;
while ( sysread (STDIN, my $rec,1) ) {
my $val = unpack ("H2",$rec);
if ( $val eq "bc" ){
seek STDIN,-1,1;
sysread (STDIN, $rec,38);
@reg = unpack ("H2 H2 H2 H2 H2 H2H2H2H2H2 H2H2H2H2H2H2H2H2H2 H2 H2H2H2H2H2H2 H2H2H2H2 H2H2H2H2 H2H2 H2H2",$rec);
for ( $i=1; $i<=38;$i++){
print $reg[$i]." ";
}
print "\n";
}
}


08 00 50 00 31 48 27 20 83 ff ff ff ff 03 87 40 55 90 20 11 07 31 05 04 29 00 00 02 75 00 00 00 00 01 21 00 16 bc
08 00 50 00 ff 87 61 17 26 ff ff ff ff ff f7 40 28 01 20 11 07 31 05 09 51 00 00 00 13 00 00 00 00 00 20 00 95 bc


I appreciate any suggestion

Thanks
malpa


 
Without delving too deeply... you cannot seek on a pipe (which is what you are reading when you pipe data in from STDIN), but you can on a file. I suspect that's your problem.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top