Skip to content

Commit 7d697bd

Browse files
author
meihuisu
committed
add a new bash diff with tolerance
1 parent 92a2c4a commit 7d697bd

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

utilities/ucvm_query_diff.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env bash
2+
# suggested by chatgpt
3+
# ucvm_query_diff.sh fileA fileB
4+
5+
file1="$1"
6+
file2="$2"
7+
tol=0.001 # change tolerance here
8+
9+
awk -v tol="$tol" '
10+
NR==FNR {
11+
lines1[NR]=$0
12+
next
13+
}
14+
{
15+
if (!(FNR in lines1)) {
16+
print "Line count differs"
17+
exit 1
18+
}
19+
20+
n1 = split(lines1[FNR], a)
21+
n2 = split($0, b)
22+
23+
if (n1 != n2) {
24+
printf("Line %d: different number of columns\n", FNR)
25+
next
26+
}
27+
28+
for (i=1; i<=n1; i++) {
29+
if (a[i] == b[i]) continue
30+
31+
# check if both are numeric
32+
if (a[i] ~ /^-?[0-9.]+$/ && b[i] ~ /^-?[0-9.]+$/) {
33+
diff = a[i] - b[i]
34+
if (diff < 0) diff = -diff
35+
if (diff > tol) {
36+
printf("Line %d, Col %d: %s != %s\n", FNR, i, a[i], b[i])
37+
}
38+
} else {
39+
printf("Line %d, Col %d: %s != %s\n", FNR, i, a[i], b[i])
40+
}
41+
}
42+
}
43+
END {
44+
if (NR != length(lines1)) {
45+
print "Line count differs"
46+
}
47+
}
48+
' "$file1" "$file2"

0 commit comments

Comments
 (0)