0

I know how to use comm, diff or grep to display every common line of two files. But how can I display only the largest common part, i. e. the most common lines in succession?

(If there are equally large common parts, I feel indifferent about which part will be displayed. Doesn't matter if first, last or undefined behavior. In my case, the largest common part will always be significantly larger than any other common part.)

Example:

A
B
C
D
E
F
G
A
X
D
E
F
B
C

Common parts are A, BC and DEF. The largest common part I'm looking for is DEF with 3 lines in comparison to A and BC with only 1 line and 2 lines.

2
  • Is there a known upper bound to the number of lines, or must we assume that either file can be arbitrarily large?
    – Jim L.
    Nov 6, 2019 at 0:21
  • I'm looking at diff for a possible tool. 1000 works. Even 10,000.
    – Jim L.
    Nov 6, 2019 at 0:26

1 Answer 1

0

This approach is based on getting the entire diff of the two files into a single context. Therefore, it makes an assumption that neither of the files will be more than 999,999 lines long.

Create this script as ./foo.sh and chmod it 755:

#!/usr/bin/env bash

max=0   # length of the highest consecutive section found
fline=  # first line of the highest consecutive section found
tline=  # first line of the current consecutive section found
n=0     # scratch counter

while IFS= read -r line
do
    case ${line:0:1} in
    ' ')    n=$(($n+1))
            [ $n -eq    1 ] && tline="${line:1}"
            [ $n -gt $max ] && { max=$n; fline="$tline"; }
            ;;
    *)      n=0
    esac
done

printf "the largest section is %d lines starting with '%s'\n" $max "$fline"

Now run diff on your files and pipe that into the shell. The shell just looks for the longest streak of lines beginning with a space, and reports the value of the first line of that section:

$ diff -u999999 file1 file2 | ./foo.sh 
the largest section is 3 lines starting with 'D'
1
  • @FineFoot the script part of this could possibly be done faster with awk but my awk-fu is not strong. And if your files are only a few hundred lines, the difference in speed may be small.
    – Jim L.
    Nov 6, 2019 at 17:40

You must log in to answer this question.

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