Is there a command to edit binaries in bulk?

Asked 1 years ago, Updated 1 years ago, 77 views

Do you have any commands to do binary editing on Linux, such as sed, awk.I would like to use this command to rewrite, for example, the number of bytes in multiple files to what values in bulk.

I searched and found a way to use echo and dd (echo is the GNU version).

$echo-ne'\x<1st byte>\x<2nd byte>...'|dd of=<file>bs=1seek=<start byte>conv=notrunc

However, with this method
1. This is how to change the original file
2. There is no check (value, range, etc.) that the content of the write is appropriate.
3. What you can do is too simple (no branching, etc.)
I felt that it was inconvenient.

linux shellscript

2022-09-30 15:42

3 Answers

How about using xxd? It looks like you can hexdump and put it back in binary.Once it becomes text, I think I can edit it.

$xxd nulls.txt
0000000: 0000 0000                                ....

$ xxd-p nulls.txt
00000000

$ xxd-p nulls.txt | seds/^0/1/ | xxd-r-p-> nulls-changed.txt

$ xxd nulls-changed.txt
0000000: 1000 0000                                ....


2022-09-30 15:42

I think it would be better to use a binary editor such as beav, but I will try GNUawk here.

GNUawk provides an extension with a readfile function that reads the entire file.Below, we read the entire file, divide it into 1 byte units, and set it to the array variable (data).After that, I changed some of them and printed them out.

$printf'\0 Kanji\nHello World\nGood night\n'>input_file
$ od-tx1c input_file
000000000 e6bca2 e5 ad 970a 48 656c 6c 6f 20576f
         \0 346 274 242 345 255 227\nHello Wo
0000020 726c 640a 476f 6f 64206e 6967 68740a
          rld\nGod night\n

$ gawk --characters-as-bytes-vf = input_file-readfile-i join'
      US>BEGIN{
        len=split(readfile(f), data, "")
        data[1] = "\x0c"; data[15] = "w"; data[26] = "N"
        printf("%s", join(data,1,len,SUBSEP))
        exit
      }
    ' |od-tx1c
0000000ce6bca2e5ad970a48656c6c6f20776f
         \f 346 274 242 345 255 227\nHello
0000020 726c 640a 476f 6f 64 204e 6967 68740a
          rld\nGod New htt\n


2022-09-30 15:42

vim-b file to open the file and :%!xxd to become a binary editor.
When saving, do :%!xxd-r and then :w.


2022-09-30 15:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.