1

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?

2
  • Do you mean like cmp(1)? Otherwise I don't understand, what you want. Mar 26, 2015 at 10:59
  • @DavidFoerster Let 00000000 01010101 10101010 ... be the binary representation of a file; I need to extract some bytes in the position I want: for example I should be able to extract 01010101 with something like var=$(getbyte 2 file); but since I need to use this to compare and output the differences between two files, the cmp solution below works perfectly for me
    – kos
    Mar 26, 2015 at 11:51

2 Answers 2

4

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
3
  • cmp is actually what I'm using to spot the differences, but I rather need to output the offending byte's value
    – kos
    Mar 25, 2015 at 11:46
  • @kos: check my edits
    – heemayl
    Mar 25, 2015 at 11:52
  • 1
    Perfect, thanks. Now I'll have to sort out a way to parse that output!
    – kos
    Mar 25, 2015 at 11:56
1

You can try with vimdiff:

vimdiff -b file1 file2

enter image description here

1
  • This is very useful. Upvoted, but I need to sort out a way to manipulate the differences within bash
    – kos
    Mar 25, 2015 at 11:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .