Skip to content

Commit 130d2e0

Browse files
committed
feat: add utils directory with diff-files.sh script
1 parent 598eef9 commit 130d2e0

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

utils/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# utils
2+
3+
These are designed to be miscellaneous utility scripts that don't fit into other categories.
4+
5+
## diff-files.sh
6+
7+
Compare and display the differences between two files

utils/diff-files.sh

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/bin/bash
2+
3+
# diff-files.sh - Find differences between two files
4+
#
5+
# Usage:
6+
# ./diff-files.sh file1 file2 # Compare two files (output to stdout)
7+
# ./diff-files.sh file1 file2 --output diff.txt # Save output to file
8+
# ./diff-files.sh file1 file2 | grep "+" # Pipe output to other commands
9+
# ./diff-files.sh --help # Show this help
10+
#
11+
# The script outputs the diff to stdout by default for maximum flexibility
12+
13+
set -e
14+
15+
# Function to show usage
16+
show_usage() {
17+
echo "Usage: $0 <file1> <file2> [options]"
18+
echo ""
19+
echo "Options:"
20+
echo " --output FILE Save output to specified file instead of stdout"
21+
echo " --help Show this help message"
22+
echo ""
23+
echo "Examples:"
24+
echo " $0 old.txt new.txt # Output to stdout"
25+
echo " $0 old.txt new.txt --output diff.txt # Save to file"
26+
echo " $0 old.txt new.txt | grep \"+\" # Pipe to other commands"
27+
}
28+
29+
30+
31+
# Parse arguments
32+
OUTPUT_FILE=""
33+
FILE1=""
34+
FILE2=""
35+
36+
while [[ $# -gt 0 ]]; do
37+
case $1 in
38+
--output)
39+
OUTPUT_FILE="$2"
40+
shift 2
41+
;;
42+
--help|-h)
43+
show_usage
44+
exit 0
45+
;;
46+
*)
47+
if [[ -z "$FILE1" ]]; then
48+
FILE1="$1"
49+
elif [[ -z "$FILE2" ]]; then
50+
FILE2="$1"
51+
else
52+
echo "Error: Too many arguments"
53+
show_usage
54+
exit 1
55+
fi
56+
shift
57+
;;
58+
esac
59+
done
60+
61+
# Check if we have both files
62+
if [[ -z "$FILE1" ]] || [[ -z "$FILE2" ]]; then
63+
echo "Error: Please provide two files to compare"
64+
echo ""
65+
show_usage
66+
exit 1
67+
fi
68+
69+
# Check if files exist
70+
if [[ ! -f "$FILE1" ]]; then
71+
echo "Error: File '$FILE1' does not exist"
72+
exit 1
73+
fi
74+
75+
if [[ ! -f "$FILE2" ]]; then
76+
echo "Error: File '$FILE2' does not exist"
77+
exit 1
78+
fi
79+
80+
# Generate diff and extract only the actual differences (+ and - lines)
81+
DIFF_OUTPUT=$(diff -u "$FILE1" "$FILE2" 2>/dev/null || true)
82+
83+
# Check if files are identical
84+
if [[ -z "$DIFF_OUTPUT" ]]; then
85+
echo "No differences found." >&2
86+
else
87+
# Filter to only show lines that were added (+) or removed (-)
88+
# Skip the header lines (---, +++, @@)
89+
FILTERED_DIFF=$(echo "$DIFF_OUTPUT" | grep -E "^[+-][^+-]")
90+
91+
if [[ -n "$FILTERED_DIFF" ]]; then
92+
LINE_COUNT=$(echo "$FILTERED_DIFF" | wc -l | tr -d ' ')
93+
94+
if [[ -n "$OUTPUT_FILE" ]]; then
95+
echo "$FILTERED_DIFF" > "$OUTPUT_FILE"
96+
echo "Saved $LINE_COUNT lines of differences to $OUTPUT_FILE" >&2
97+
else
98+
echo "$FILTERED_DIFF"
99+
echo "Found $LINE_COUNT lines of differences" >&2
100+
fi
101+
else
102+
echo "No content differences found." >&2
103+
fi
104+
fi

0 commit comments

Comments
 (0)