Shell – dd remove range of bytes

ddshell-script

Given this file

$ cat hello.txt
hello doge world

I would like to remove a range of bytes to end up with this

$ cat hello.txt
heorld

I would like to do this with dd if possible. The reason is because I am
already using dd to overwrite bytes in this manner

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

I prefer to write back to the same file, but a different output file would be
okay.

Best Answer

It is a matter of specifying blocksize, count, and skip:

$ 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

The above uses three invocations of dd. The first gets the first two characters he. The second skips to the end of hello and copies the space which follows. The third skips into the last word world copying all but its first character.

This was done with GNU dd but BSD dd looks like it should work also.

Related Question