如何从指定的偏移量输出文件,而不是“dd bs=1 skip=N”?

Vi.*_*Vi. 37 linux dd

如何dd if=somefile bs=1 skip=1337 count=31337000不使用非 1 字节的读取和写入,但要高效地做这样的事情?

预计解决方案:

  1. 为了简单(对于非简单,我可以编写一些 Perl oneliner 来做到这一点)
  2. 支持大的偏移量和长度(所以在 dd 中使用块大小的 hack 无济于事)

部分解决方案(不够简单,尝试相同的长度会使其更加复杂):

dd if=somefile bs=1000 skip=1 count=31337 | { dd bs=337 count=1 of=/dev/null; rest_of_pipeline; }
# 1337 div 1000 and 1337 mod 1000
Run Code Online (Sandbox Code Playgroud)

Fab*_*ano 50

这应该这样做(在 gnu dd 上):

dd if=somefile bs=4096 skip=1337 count=31337000 iflag=skip_bytes,count_bytes
Run Code Online (Sandbox Code Playgroud)

如果您也在使用seek=,您也可以考虑oflag=seek_bytes.

来自info dd

`count_bytes'
      Interpret the `count=' operand as a byte count, rather than a
      block count, which allows specifying a length that is not a
      multiple of the I/O block size.  This flag can be used only
      with `iflag'.

`skip_bytes'
      Interpret the `skip=' operand as a byte count, rather than a
      block count, which allows specifying an offset that is not a
      multiple of the I/O block size.  This flag can be used only
      with `iflag'.

`seek_bytes'
      Interpret the `seek=' operand as a byte count, rather than a
      block count, which allows specifying an offset that is not a
      multiple of the I/O block size.  This flag can be used only
      with `oflag'.
Run Code Online (Sandbox Code Playgroud)

Ps:我知道这个问题很旧,似乎这些标志是在最初提出问题后实施的,但由于它是我所做的相关 dd 搜索的第一个谷歌结果之一,我虽然更新新的会很好特征。