首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >perl在binmode中查找和删除不同的偏移量。

perl在binmode中查找和删除不同的偏移量。
EN

Stack Overflow用户
提问于 2013-02-09 20:15:08
回答 1查看 1.4K关注 0票数 2

这是我正在写的剧本。

代码语言:javascript
复制
#usr/bin/perl
use warnings;


open(my $infile, '<', "./file1.bin") or die "Cannot open file1.bin: $!";
binmode($infile);
open(my $outfile, '>', "./extracted data without 00's.bin") or die "Cannot create extracted data without 00's.bin: $!";
binmode($outfile);

local $/; $infile = <STDIN>;
   print substr($infile, 0, 0x840, '');
   $infile =~ s/\0{16}//;
   print $outfile;

我正在用perl加载一个二进制文件。我已经能够在某些偏移量处查找和修补,但我想要做的是,现在能够找到"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00“的任何实例(16个字节?)并将其从文件中删除,但不少于16字节。如果不是我想离开的话。在某些文件中,00开始位置的偏移量将处于不同的偏移量,但如果我的想法正确,如果我只需搜索00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00并删除它的任何实例,那么00的偏移量就不重要了。我首先从特定的偏移量中提取数据,然后搜索文件并从中删除00。我已经可以提取我需要的特定偏移量了,我只需要打开解压缩的文件并刮掉00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

代码语言:javascript
复制
EF 39 77 5B 14 9D E9 1E 94 A9 97 F2 6D E3 68 05
6F 7B 77 BB C4 99 67 B5 C9 71 12 30 9D ED 31 B6 
AB 1F 81 66 E1 DD 29 4E 71 8D 54 F5 6C C8 86 0D 
5B 72 AF A8 1F 26 DD 05 AF 78 13 EF A5 E0 76 BB 
8A 59 9B 20 C5 58 95 7C E0 DB 44 6A EC 7E D0 10 
09 42 B1 12 65 80 B3 EC 58 1A 2F 92 B9 32 D9 07 
96 DE 32 51 4B 5F 3B 50 9A D1 09 37 F4 6D 7C 01 
01 4A A4 24 04 DC 83 08 17 CB 34 2C E5 87 26 C1 
35 38 F4 C4 E4 78 FE FC A2 BE 99 48 C9 CA 69 90 
33 87 09 A8 27 BA 91 FC 4B 77 FA AB F5 1E 4E C0        I want to leave everything from
F2 78 6E 31 7D 16 3B 53 04 8A C1 A8 4B 70 39 22 <----- here up
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 <----- I want to prune everything
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00        from here on
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00<---- this IS the end of the file, and
                                                     just need to prune these few rows
                                                     of 00's

假设上面示例中的"F2 78 6E“位于偏移量( 0x45000 ),但在另一个文件( 00 )中的将以不同的偏移量开始,我如何对它进行编码,以使00 00's得到修剪。在我打开的任何文件里?如果我需要说得更具体点,就问吧。似乎我会一直偷看文件,直到我命中一个长的00字符串,然后修剪任何剩余的行。这说得通吗?我所要做的就是搜索文件中的任何00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00实例,并删除/修剪/截断它。我想拯救所有的一切,除了00

编辑#2 --它完成了以下工作:

代码语言:javascript
复制
open($infile, '<', './file1') or die "cannot open file1: $!";
binmode $infile;
open($outfile, '>', './file2') or die "cannot open file2: $!";
binmode $outfile;

local $/; $file = <$infile>;
$file =~ s/\0{16}//g;
print $outfile $file;


close ($infile);
close ($outfile);

谢谢ikegami 的帮助和耐心:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-09 20:20:48

没有从文件中移除的东西。你要么

  1. 复制文件而不需要不需要的位,或
  2. read文件的其余部分,seek返回,print对不想要的位,然后truncate文件。

我采用了备选方案1。

代码语言:javascript
复制
$ perl -e'
   binmode STDIN;
   binmode STDOUT;
   local $/; $file = <STDIN>;
   $file =~ s/\0{16}//;
   print $file;
' <file.in >file.out

我要把整个文件加载到内存中。这两个选项都可以用块来完成,但这会使事情复杂化,因为空值可以跨越两个块。

在措辞不佳的更新中,您似乎要求避免更改第一个0x840字节。有两种解决办法:

代码语言:javascript
复制
$ perl -e'
   binmode STDIN;
   binmode STDOUT;
   local $/; $file = <STDIN>;
   substr($file, 0x840) =~ s/\0{16}//;
   print $file;
' <file.in >file.out

$ perl -e'
   binmode STDIN;
   binmode STDOUT;
   local $/; $file = <STDIN>;
   print substr($file, 0, 0x840, '');
   $file =~ s/\0{16}//;
   print $file;
' <file.in >file.out
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14791108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档