'Count character difference between two files in bash
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
Solution 1:[1]
Use cmp to compare files with -l for verbose, then count the lines with wc:
cmp -bl file1 file2 | wc -l
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Stephan van der Feest |
| Solution 2 | Jalan |
