So, I am attempting to read all binary files in a directory, seek to a header section, advance the position 94 bytes, and then change the next 10 bytes. With all of my research I have gotten close with the code below but syswrite is looking for numeric information. I should probably also be performing the replacement on a copy of the file but it could be added later. Anyone who knows the simplest method of replacing the 10 bytes in the syswrite statement has my appreciation and thanks.
#!/usr/bin/perl use strict; use warnings; use Data::HexDump; my $tktdir = "/home/bkirch/PERL_LSN/TICKETS"; my $file; my $offset; opendir(TKS, $tktdir) || die "Oops ... $!"; my @files = readdir TKS; close TKS; foreach my $file (@files) { open(FH,"+<$tktdir/$file") or die "Oops - Cant open ticket $!"; binmode FH; while (<FH>) { if (/SCSI:INQ:80/) { $offset = tell (FH); # print "$offset\n"; #seek (FH, $offset, 94); seek (FH, $offset, 94); $offset = tell (FH); # print "$offset\n"; #read(FH, $SN, 10); sysread (FH, $offset, 10); # print "$offset\n"; my $BUF = ""; syswrite (FH, $BUF, 10, $offset); print "$BUF\n"; # s[$offset][TEST123456]g; } } }
Thanks JavaFan and graff for responding to my question. You can tell I am a beginner by the print debug statements in the code. When the code is run it checks the binary file for the pattern match and the first instance of "tell" actually places a pointer at the first instance of "S" in SCSI. seek moves the pointer forward 94 bytes and the second instance of tell resets the pointer at the beginning character of the data to be replaced. This is verified by the (commented out read statement when the program is run). In trying JavaFan's solution I run into errors (on Windows) with regard to the options used (-pi -e). I hope to run this routine on both Windows and Linux. I will keep trying but again thanks for your suggestions. -pbyfire
In reply to Changing data with syswrite ? by pbyfire
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |