2

I would like to use a linux shell (bash, zsh, etc.) to insert a set of known bytes into a file at a certain position. Similar questions have been asked, but they modify in-place the bytes of a file. These questions don't address inserting new bytes at particular positions.

For example, if my file has a sequence of bytes like \x32\x33\x35 I might want to insert \x34 at position 2 so that this byte sequence in the file becomes \x32\x33\x34\x35.

1 Answer 1

3

You can achieve this using head, tail and printf together. For example; to insert \x34 at position 2 in file:

{ head -c 2 file; printf '\x34'; tail -c +3 file; } > new_file

For POSIX-compliance, \064 (octal representation of \x34) can be used.

To make this change in-place, just move new_file to file.


No matter which tool(s) you use, this operation will cost lots of CPU time for huge files.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.