dd question

Garry T. Williams gtwilliams at gmail.com
Fri Dec 10 23:37:25 UTC 2010


On Thursday, December 09, 2010 22:11:25 Amadeus W.M. wrote:
> I have a binary file with data. Each block of 48 bytes is a record.

See perlfunc(1) and the sysread() Perl builtin.  The easiest way to do
this would be to loop over calls to sysread() that specify 48
characters.  Here's the whole thing:

    use warnings;
    use strict;

    exit unless @ARGV;

    foreach my $file (@ARGV) {
	open my $fh, $file or die "can't open $file: $!\n";

	my ($rc, $rec);
	while ($rc = sysread($fh, $rec, 48)) {
	    # do something with $rec
	}

	die "read failed: $!\n" unless defined $rc;
    }

> I want to extract the first 8 bytes within each record.

    substr $rec, 0, 8

-- 
Garry Williams


More information about the users mailing list