replacing values of a file using hex editing script

1

I need help to create a bash script that will copy three lines of code from inside a savegame (sav) file and paste them into another savegame file. I have used a hex editor to do this manually by pasting the values at the same offset, but would like to have a script that does this automatically.

Example:

Source file has these values when viewed in a HEX editor.

  1. 00001E40 - AC 59 D2 01 00 DC AB 8B
  2. 00001E50 - DC 54 D2 01 D0 FE 1A 00
  3. 000FFA90 - 4C 00 00 00 01 14 02 00

I want to copy & paste these values to the same offset in a new file.

I am not a coder and don't know too much about scripting, but want to know is this possible to do?

Kalamalka Kid

Posted 2017-02-28T19:54:37.343

Reputation: 370

1"from inside a hex file" - there are no "hex files", hex editors shows content of any file in hexadecimal, your question is unclear now. Please describe a bit more detailed what do you need to do. Also please note that this isn't a free code writing service. Questions asking for entire programs are likely to be closed as too broad. – Máté Juhász – 2017-02-28T20:07:26.373

1ok, edited thank you. DO you think the request is more clear now? – Kalamalka Kid – 2017-02-28T20:14:59.843

1This is fairly elementary programming, with a clear goal. IMO, this is a perfect time for you to learn. You need to open a file for binary access, read and store 24 bytes, close file; open second (2) file, and a third (3-new) file for binary access, read byte from (2), write byte to (3) up to the offset, then write the stored 24 bytes to (3), then advance 24 bytes in (2) and then read'n'write till the end of file marker for (2); close 2 and 3. So google for "Bash binary open and read file loop". Once you have something, you can then ask for help with problems – Yorik – 2017-02-28T20:21:15.820

Please note that https://superuser.com is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?.

– DavidPostill – 2017-02-28T20:25:39.827

@Yorik thank you.. please tell me though, the 3rd line of data (000FFA90) is at the end of the file while the first two (00001E40 & 00001E50) are located in the middle. Does the process you wrote take this into account? – Kalamalka Kid – 2017-02-28T21:33:59.087

1No. So you need to modify your process. But the first step is to "identify how you identify" the information: is it a fixed location (the last 8 bytes exactly), is it a unique series? etc. There is a thing called Rubber Duck Debugging which is also useful in how you plan a process: explain what you are doing to a rubber duck. Then write out that plan, then encode that using a programming language. Just bear in mind that computers are the most pedantic people you know so you need to be very specific. – Yorik – 2017-02-28T21:39:15.827

1If they are fixed locations in all files always, then the "000FFA90" is the exact location, so you can usually do a read from a specific offset. This is going to be a lot quicker and easier than storing the previous eight bytes you read and comparing it to a specific byte sequence. – Yorik – 2017-02-28T21:47:25.183

No answers