Page 1 of 1

XOR One File With Another

Posted: 2021-Jul-25, 12:46 pm
by MigrationUser
07 Sep 2012 18:32
jamesadrian

I am relearning BASH after years away from it and I never knew very much about it. I am looking for a way to take two files of equal size and create a third file where the nth byte in the third file is the nth byte in the first file XOR'ed with the nth byte in the second file. I don't want to learn every last detail of BASH before I get to a solution. My need is very specific.

I can use the terminal in OS X to display directories and list the files they contain. I can use nano to write a BASH script. What sort of things should I be looking for to keep this from becoming a year-long project?

Thank you for your help.

Jim Adrian

----------------------------

#2 07 Sep 2012 23:07
jamesadrian
jamesadrian wrote:

I am relearning BASH after years away from it and I never knew very much about it. I am looking for a way to take two files of equal size and create a third file where the nth byte in the third file is the nth byte in the first file XOR'ed with the nth byte in the second file. I don't want to learn every last detail of BASH before I get to a solution. My need is very specific.

I can use the terminal in OS X to display directories and list the files they contain. I can use nano to write a BASH script. What sort of things should I be looking for to keep this from becoming a year-long project?

Thank you for your help.

Jim Adrian

The XOR operator is "^" so some thing like x ^ y gets calculated, just as perhaps X + Y could be calculated. Does the result replace one of the variables or is there a command like z = x + y that does not modify either x nor y?

Thank you for your help.

Jim Adrian


----------------------------

#3 16 Sep 2012 21:03
zedmelon
jamesadrian wrote:

I am looking for a way to take two files of equal size and create a third file where the nth byte in the third file is the nth byte in the first file XOR'ed with the nth byte in the second file. I don't want to learn every last detail of BASH before I get to a solution. My need is very specific.

I can use the terminal in OS X to display directories and list the files they contain. I can use nano to write a BASH script. What sort of things should I be looking for to keep this from becoming a year-long project?
Working from a specific example someone might offer a more elegant solution that what I propose below. Without knowing more detail (file structure regularity, whether nth byte is always numeric), I'd determine three things for each source file: content before nth byte, nth byte, content after nth byte.

1a. It seemed to me that you might find the nth byte with sed, but I couldn't find a way to do that. Someone more familiar with advanced usage of sed may (and well should) put me in my place for that. The options I tried only worked on a line-by-line basis, which seems to counter the 'stream editor' paradigm. Again, someone should correct me; I was hoping to learn a new feature of sed in answering your question. :-(

1b. If using sed doesn't work--and my gut still maintains that it should--the remaining process becomes slightly more complicated as you first isolate the line containing the byte you need to manipulate. A proper sed command can whisk you to step four.

2a. If your files have a regular enough structure, and the nth byte is always on the same line, you could cat the file into 'head -L' (where L is the line number you need) and then 'tail -1' (which reduces the previous output to one line).

2b. Even better, if the nth byte is always seen adjacent to the same string, you can grep for that string and reduce the file to one line.

2c. If neither of these is true, I'm flummoxed as to how the nth byte can be reliably determined (and also how one would know it needs processed by byte index). In that case your only recourse might be to pipe whatever generates these files through a script functioning as a fifo and which counts bytes as they enter. This script would ultimately generate the files you describe above as the source (along with simultaneously generating the third file). This would be significantly more complex than the Cliffs Notes solution you're requesting.

3. Once you're working with a single line, you can use cut to print the nth byte

cut -c 10

4. Perform your XOR operation as desired, and slap the pieces back together for the output file.