null_reflections
Legendary Coder
Here is a bash script (on windows, i think "git for windows" would allow you to run this, the commands are typically native to linux...) that allows you to print lines that match or are different in a file:
Code:
#!/bin/bash
#easier to read format for showing differences between files
SCRIPT=$0
#instructions in how to use script
if [ $# -lt 3 ]; then
echo "Usage: ${SCRIPT##*/} -[option(s)] file file2"
echo "option -u: print unique lines in documents"
echo "option -c: print different lines side-by-side"
echo "option -s: print lines that are the same side-by-side"
exit
fi
#instructions for script, ensures that proper argument is included
if ! [[ "$1" =~ ^-[ucs] ]]; then
echo "Usage: ${SCRIPT##*/} -[option(s)] file file2"
echo "-u option: print unique lines in documents"
echo "-c option: print different lines side-by-side"
echo "-s option: print lines that are the same side-by-side"
exit
fi
#error if either file doesn't exist, specific
if ! [ -f "$2" ] || ! [ -f "$3" ]; then
if ! [ -f "$2" ] && ! [ -f "$3" ]; then
echo "File $2 and $3 do not exist as regular files."
exit 2
fi
if ! [ -f "$2" ] ; then
echo "File $2 does not exist as a regular file."
exit 2
fi
if ! [ -f "$3" ] ; then
echo "File $3 does not exist as a regular file."
exit 2
fi
fi
sed 's/^[ \t]*//g' $2 > temp-diff
sed 's/^[ \t]*//g' $3 > temp-diff2
#assigns lines from file to arrays
readarray -t DIFF < temp-diff
readarray -t DIFF2 < temp-diff2
#if $1 contains "-u", then print unique lines
if [[ "$1" = "-"*"u"* ]]; then
echo -e "These lines are unique to $2:\n"
diff --new-line-format="" --unchanged-line-format="" <(sort temp-diff) <(sort temp-diff2) | sed '/^$/d
'
echo -e "\nThese lines are unique to $3:\n"
diff --new-line-format="" --unchanged-line-format="" <(sort temp-diff2) <(sort temp-diff) | sed '/^$/d
'
fi
#if $1 contains "-c", then compare side by side
if [[ "$1" = "-"*"c"* ]]; then
echo -e "Different lines in $1:\n"
#loops non-matching lines in an array plus their line number from first file
count=${#DIFF[@]}
for (( j=0; j<count; j++ )); do
line_numb=$((j + 1))
if [ "${DIFF[j]}" != "${DIFF2[j]}" ]; then
echo "${DIFF[j]} (Line $line_numb)"
fi
done | sed '/^$/d'
#loops non-matching lines in an array from their line number from second file
echo -e "\nDifferent lines in $2:\n"
count2=${#DIFF2[@]}
for (( j=0; j<count2; j++ )); do
line_numb=$((j + 1))
if [ "${DIFF[j]}" != "${DIFF2[j]}" ]; then
echo "${DIFF2[j]} (Line $line_numb)"
fi
done | sed '/^$/d'
fi
#if line contains "-s", then display lines that are the same when compared side by side
if [[ "$1" = "-"*"s"* ]]; then
echo -e "\nThese lines are exactly the same in both:\n"
count3=${#DIFF[@]}
for (( j=0; j<count3; j++ )); do
if [ "${DIFF[j]}" = "${DIFF2[j]}" ] && [ -n "${DIFF2[j]}" ]; then
echo "${DIFF[j]}"
fi
done
fi
#remove files used to create arrays earlier
rm temp-diff temp-diff2