Created
May 28, 2025 03:12
-
-
Save insaneyilin/a82faf292753fefbf971e1967d8d423b to your computer and use it in GitHub Desktop.
Find diff lines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check number of arguments | |
if [ $# -ne 3 ]; then | |
echo "Usage: $0 <file1> <file2> <output_file>" | |
echo "Example: $0 file1.txt file2.txt differences.txt" | |
exit 1 | |
fi | |
# Get command line arguments | |
FILE1="$1" | |
FILE2="$2" | |
OUTPUT="$3" | |
# Check if input files exist | |
if [ ! -f "$FILE1" ]; then | |
echo "Error: File $FILE1 does not exist" | |
exit 1 | |
fi | |
if [ ! -f "$FILE2" ]; then | |
echo "Error: File $FILE2 does not exist" | |
exit 1 | |
fi | |
# Create temporary directory | |
TEMP_DIR=$(mktemp -d) | |
FILE1_SORTED="$TEMP_DIR/file1_sorted.txt" | |
FILE2_SORTED="$TEMP_DIR/file2_sorted.txt" | |
FILE1_UNIQUE="$TEMP_DIR/file1_unique.txt" | |
FILE2_UNIQUE="$TEMP_DIR/file2_unique.txt" | |
# Cleanup function | |
cleanup() { | |
rm -rf "$TEMP_DIR" | |
} | |
# Set cleanup on exit | |
trap cleanup EXIT | |
# Sort files (comm requires sorted input) | |
sort "$FILE1" -o "$FILE1_SORTED" | |
sort "$FILE2" -o "$FILE2_SORTED" | |
# Find lines unique to file1 | |
comm -23 "$FILE1_SORTED" "$FILE2_SORTED" > "$FILE1_UNIQUE" | |
# Find lines unique to file2 | |
comm -13 "$FILE1_SORTED" "$FILE2_SORTED" > "$FILE2_UNIQUE" | |
# Combine all different lines to output file | |
cat "$FILE1_UNIQUE" "$FILE2_UNIQUE" > "$OUTPUT" | |
echo "Differences saved to: $OUTPUT" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment