Tuesday, May 14, 2024
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 1715  / 3 Years ago, mon, august 9, 2021, 10:00:28

Since two persons already voted to close this post, I'll try to make the question as clear as possible. A file on a physical device is represented as a binary sequence. That being said, what I need to do is to check the differences between the representations of two files at a such level (i.e. I need to compare the differences between multiple bytes on the same position across two different files), and to output such differences in a bash script.



Example:



file1: 00000000 01010101 10101010 11001100 00110011

file2: 00000000 01010101 01010101 00110011 00110011


The script should output:



differences: Byte 3 (file 1: 01010101; file2 10101010), Byte 4 (file1: 11001100; file 2: 00110011)


Or something along these lines.



So the first thing I need to do in order to accomplish this is to be at least able to open a file at a certain position and to read one byte and to output/store such byte. I could write a C program to do that, but is there a way to do this within bash?


More From » command-line

 Answers
2

You can try cmp. It will compare two files byte by byte.



From man cmp:



cmp - compare two files byte by byte


Although the number of lines must be equals on two files. Also note that cmp will point to the first difference only, to point to the next differences you can skip specific bytes from the start.



$ cat foo 
this is
a test
$ cat bar
this
is a test
$ cmp foo bar
foo bar differ: byte 5, line 1
$ cmp -b foo bar
foo bar differ: byte 5, line 1 is 40 12 ^J


To print the differing byte values use cmp -l, from man cmp:



   -l, --verbose
output byte numbers and differing byte values

[#20791] Wednesday, August 11, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
utonmbo

Total Points: 134
Total Questions: 104
Total Answers: 118

Location: Argentina
Member since Mon, Jan 3, 2022
2 Years ago
;