Skip to content

Instantly share code, notes, and snippets.

@realyukii
Created March 22, 2024 02:58
Show Gist options
  • Save realyukii/812b036b8e02f1b15f977e12c5462a94 to your computer and use it in GitHub Desktop.
Save realyukii/812b036b8e02f1b15f977e12c5462a94 to your computer and use it in GitHub Desktop.
Script for diff the paragraph
#!/bin/bash
# used for checking grammar, it uses Internal Field Separator
# related link: https://stackoverflow.com/questions/454427/string-difference-in-bash
# todo: figuring out a better algorithm than checking word by word in a linear fashion
# Function to compare two words
compare_words() {
local word1="$1"
local word2="$2"
if [[ "$word1" != "$word2" ]]; then
echo "Difference found: '$word1' vs '$word2'"
fi
}
# Main script
# Save strings as arguments or read from user input
if [ $# -eq 2 ]; then
str1="$1"
str2="$2"
else
echo "Enter string 1:"
read str1
echo "Enter string 2:"
read str2
fi
# Split strings into words using IFS
IFS=' ' read -r -a words1 <<< "$str1"
IFS=' ' read -r -a words2 <<< "$str2"
# Loop through words and compare them
for (( i=0; i<${#words1[@]}; i++ )); do
if [[ ${#words2[@]} -gt $i ]]; then
compare_words "${words1[$i]}" "${words2[$i]}"
else
echo "Extra word in string 1: '${words1[$i]}'"
fi
done
# Check for extra words in string 2
for (( i=${#words1[@]}; i<${#words2[@]}; i++ )); do
echo "Extra word in string 2: '${words2[$i]}'"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment