#!/usr/bin/env bash
set -euo pipefail
# Usage:
# ./compare-oids.sh file_a.txt file_b.txt
#
# The script:
# - ignores blank lines;
# - removes leading and trailing whitespace;
# - ignores duplicate OIDs;
# - reports OIDs found only in A and only in B.
if [[ $# -ne 2 ]]; then
echo "Usage: $0 <file_a> <file_b>" >&2
exit 1
fi
FILE_A="$1"
FILE_B="$2"
if [[ ! -f "$FILE_A" ]]; then
echo "Error: file does not exist: $FILE_A" >&2
exit 1
fi
if [[ ! -f "$FILE_B" ]]; then
echo "Error: file does not exist: $FILE_B" >&2
exit 1
fi
TEMP_DIR="$(mktemp -d)"
cleanup() {
rm -rf "$TEMP_DIR"
}
trap cleanup EXIT
SORTED_A="$TEMP_DIR/a.sorted"
SORTED_B="$TEMP_DIR/b.sorted"
ONLY_A="$TEMP_DIR/only-a"
ONLY_B="$TEMP_DIR/only-b"
# Normalize both files:
# 1. Remove leading and trailing whitespace.
# 2. Remove blank lines.
# 3. Sort numerically by OID components.
# 4. Remove duplicates.
normalize_file() {
local input_file="$1"
local output_file="$2"
sed 's/^[[:space:]]*//; s/[[:space:]]*$//' "$input_file" |
grep -v '^[[:space:]]*$' |
sort -Vu > "$output_file"
}
normalize_file "$FILE_A" "$SORTED_A"
normalize_file "$FILE_B" "$SORTED_B"
# comm columns:
# -23: lines only in A
# -13: lines only in B
comm -23 "$SORTED_A" "$SORTED_B" > "$ONLY_A"
comm -13 "$SORTED_A" "$SORTED_B" > "$ONLY_B"
echo "Only in A ($FILE_A):"
if [[ -s "$ONLY_A" ]]; then
cat "$ONLY_A"
else
echo "(none)"
fi
echo
echo "Only in B ($FILE_B):"
if [[ -s "$ONLY_B" ]]; then
cat "$ONLY_B"
else
echo "(none)"
fi
echo
echo "Summary:"
echo "Only in A: $(wc -l < "$ONLY_A")"
echo "Only in B: $(wc -l < "$ONLY_B")"
if [[ ! -s "$ONLY_A" && ! -s "$ONLY_B" ]]; then
echo "The files contain the same set of OIDs."
fi