简体   繁体   中英

compare binary files and print only offset of matching line

For regular files I can use the comm command to find common lines.

For example we have two files

$ cat f1
line1
line2
line3
line4
line5

$ cat f2
line1
line20
line30
line4
line5

Its compared like:

$ comm -12 f1 f2
line1
line4
line5

How to find the offset of matching lines and also how to do comparison for two binary files and print matching line offset?

I've been using things like diff , cmp , comm for past 1hr, unable to figure this out.

EDIT 1: Not exact solution but found vbindiff it helps a bit.

You are probably looking for cmp :

cmp - compare two files byte by byte

$ cmp f1 f2
f1 f2 differ: byte 12, line 2

$ cmp -b f1 f2
f1 f2 differ: byte 12, line 2 is  12 ^J  60 0

$ cmp -bl f1 f2
12  12 ^J    60 0
13 154 l     12 ^J
14 151 i    154 l
15 156 n    151 i
16 145 e    156 n
17  63 3    145 e
18  12 ^J    63 3
19 154 l     60 0
20 151 i     12 ^J
21 156 n    154 l
22 145 e    151 i
23  64 4    156 n
24  12 ^J   145 e
25 154 l     64 4
26 151 i     12 ^J
27 156 n    154 l
28 145 e    151 i
29  65 5    156 n
30  12 ^J   145 e
cmp: EOF on f1

From man cmp :

-b , --print-bytes

print differing bytes

-l , --verbose

output byte numbers and differing byte values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM