Home > other >  Count character difference between two files in bash
Count character difference between two files in bash

Time:05-03

What would be the best way to count the number of characters that are different between strings in two files. I know how to do this in Python but I need a bash solution.

File contents:

ABCDEF
ABDCEF

Output:

2

Thank you

CodePudding user response:

Use cmp to compare files with -l for verbose, then count the lines with wc:

cmp -bl file1 file2 | wc -l

http://linux.die.net/man/1/cmp

CodePudding user response:

The following worked,

#!/bin/bash
read -p "Filename: " file1
    awk '(NR>1)' $file1 | tee file1.tmp
        for file in *.txt
            do awk '(NR>1)' $file > $file2.tmp
            cmp -bl file1.tmp $file2.tmp | wc -l
            rm $file2.tmp
        done

function finish {
    rm file1.tmp
}
trap finish EXIT
  • Related