dd 删除字节范围

Ste*_*nny 6 dd shell-script

鉴于此文件

$ cat hello.txt
hello doge world
Run Code Online (Sandbox Code Playgroud)

我想删除一系列字节以结束这个

$ cat hello.txt
heorld
Run Code Online (Sandbox Code Playgroud)

dd如果可能的话,我想这样做。原因是因为我已经使用dd这种方式覆盖字节

printf '\x5E' | dd conv=notrunc of=hello.txt bs=1 seek=$((0xE))
Run Code Online (Sandbox Code Playgroud)

我更喜欢写回同一个文件,但不同的输出文件也可以。

Joh*_*024 5

这是指定块大小、计数和跳过的问题:

$ cat hello.txt
hello doge world
$ { dd bs=1 count=2 ; dd skip=3 bs=1 count=1 ; dd skip=6 bs=1 ; } <hello.txt 2>/dev/null
he orld
Run Code Online (Sandbox Code Playgroud)

以上使用了三个调用dd。第一个获取前两个字符he。第二个跳到末尾hello并复制后面的空格。第三个跳到最后一个单词,world复制除第一个字符以外的所有字符。

这是用GNUdd完成的,但BSDdd看起来也应该可以工作。


Ste*_*nny 5

# copy the end piece into correct position
dd bs=1 seek=2 skip=12 conv=notrunc if=hello.txt of=hello.txt

# truncate
dd bs=1 seek=6 if=/dev/null of=hello.txt
Run Code Online (Sandbox Code Playgroud)

马克是对的