How do I extract a single chunk of bytes from within a file?

后端 未结 5 1228
面向向阳花
面向向阳花 2020-12-07 16:01

On a Linux desktop (RHEL4) I want to extract a range of bytes (typically less than 1000) from within a large file (>1 Gig). I know the offset into the file and the size of t

相关标签:
5条回答
  • 2020-12-07 16:43

    Try dd:

    dd skip=102567 count=253 if=input.binary of=output.binary bs=1
    
    0 讨论(0)
  • 2020-12-07 16:46

    head -c + tail -c

    Not sure how it compare to dd in efficiency, but it is fun:

    printf "123456789" | tail -c+2 | head -c3
    

    picks 3 bytes, starting at the 2nd one:

    234
    

    See also: https://stackoverflow.com/a/1272995/895245

    0 讨论(0)
  • 2020-12-07 16:46

    The dd command can do all of this. Look at the seek and/or skip parameters as part of the call.

    0 讨论(0)
  • 2020-12-07 16:50

    This is an old question, but I'd like to add another version of the dd command that is better-suited for large chunks of bytes:

    dd if=input.binary of=output.binary skip=$offset count=$bytes iflag=skip_bytes,count_bytes 
    

    where $offset and $bytes are numbers in byte units.

    The difference with Thomas's accepted answer is that bs=1 does not appear here. bs=1 produces the input and output block size to be 1 byte, which makes it terribly slow when the number of bytes to extract is large.

    0 讨论(0)
  • 2020-12-07 17:08

    Even faster

    dd bs=<req len> count=1 skip=<req offset> if=input.binary of=output.binary 
    
    0 讨论(0)
提交回复
热议问题