0

I have binary file that I want to replace with shell script 0xaa,0xbb,0xcc,0xdd with 0x11,0x22,0x33,0x44

I can get 0xaa,0xbb,0xcc,0xdd into shell script like $1 but how can I replace it with 0x11,0x22,0x33,0x44 without sed?

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
  • 4
    Welcome to SO, please be more clear in your question(like which script you are talking about, please do mention it in your question). Also it is always recommended to mention 3 important things 1- Sample of input, 2- Sample of output 3- efforts which you have put in order to solve your problem in code tags. Please add them clearly and do let us know. – RavinderSingh13 Feb 04 '19 at 04:22
  • This question has been cross-posted on AskUbuntu: https://askubuntu.com/questions/1115358/replace-hex-in-binary-file-with-shell-without-sed – John1024 Feb 04 '19 at 05:46
  • 2
    what is the issue in using `sed` ? – Jithin Scaria Feb 04 '19 at 12:07

1 Answers1

1

You could create a bash script that does a hexdump of your file, a simple replace and then convert back to a binary file.

You could use an application such as xxd, or hexdump to create a hex dump of your file. This is explained here: convert binary data to hex in shell script?

It looks like xxd creates a hex string that looks like this "aabbccdd" rather than your "0xaa,0xbb,0xcc,0xdd" so watch out for that!

To find out how to do a replace, you could look at this answer: Find and Replace Inside a Text File from a Bash Command

You would then want to convert the HEX string back into binary, which is explained here: linux shell scripting: hex string to bytes