如何从Unix / Linux上的文件抓取任意块 [英] How to grab an arbitrary chunk from a file on unix/linux

查看:128
本文介绍了如何从Unix / Linux上的文件抓取任意块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一大块从一个二进制文件复制到一个新文件。我已经字节偏移和块长度我要抢。

I'm trying to copy a chunk from one binary file into a new file. I have the byte offset and length of the chunk I want to grab.

我已经使用 DD 工具试过,不过这似乎读取和丢弃数据高达偏移,而不仅仅是追求(我猜是因为dd是复制/变换数据的块)。这使得它很慢(速度较慢的抵消较高这是我试过的命令:

I have tried using the dd utility, but this seems to read and discard the data up to the offset, rather than just seeking (I guess because dd is for copying/converting blocks of data). This makes it quite slow (and slower the higher the offset. This is the command I tried:

dd if=inputfile ibs=1 skip=$offset count=$datalength of=outputfile

我想我可以写一个小的Perl / Python的/不管脚本打开该文件,寻求补偿,那么读写块所需的数据量。

I guess I could write a small perl/python/whatever script to open the file, seek to the offset, then read and write the required amount of data in chunks.

有没有支持这样的一个工具?

Is there a utility that supports something like this?

推荐答案

是的,它是尴尬今天做这与日。我们正在考虑增加skip_bytes和count_bytes PARAMS中的coreutils以帮助DD。以下应工作,虽然:

Yes it's awkward to do this with dd today. We're considering adding skip_bytes and count_bytes params to dd in coreutils to help. The following should work though:

#!/bin/sh

bs=100000
infile=$1
skip=$2
length=$3

(
  dd bs=1 skip=$skip count=0
  dd bs=$bs count=$(($length / $bs))
  dd bs=$(($length % $bs)) count=1
) < "$infile"

这篇关于如何从Unix / Linux上的文件抓取任意块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆