Updated, fixed issues, added new sorting script

This commit is contained in:
G2-Games 2022-10-24 03:17:12 -05:00
parent 577143b1d4
commit c75c81588b
2 changed files with 15 additions and 3 deletions

View file

@ -1,16 +1,22 @@
#!/bin/bash #!/bin/bash
word=$(echo $1 | xargs) word=$(echo $1 | xargs)
dictionary="${2:-words.txt}"
if [[ $word = "" ]]; then if [[ $word = "" ]]; then
echo "usage: ./anagram.sh <word>" echo "usage: ./anagram.sh <word> <dictionary file, optional>"
exit 1 exit 1
fi fi
if [ ! -f "sorted$dictionary" ]; then
echo "Sorted dictionary not found. Generating (this will take a while)..."
bash sorter.sh $dictionary
echo "Done!"
fi
wordsorted=$(echo $word | grep -o . | sort | tr -d "\n") wordsorted=$(echo $word | grep -o . | sort | tr -d "\n")
lines=$(grep -xin -- "$wordsorted" sortedwords.txt | sed -e 's/:.*//g') lines=$(grep -xin -- "$wordsorted" sorted$dictionary | sed -e 's/:.*//g')
if [[ $lines = "" ]]; then if [[ $lines = "" ]]; then
echo "No matches." echo "No matches."
exit 0 exit 0
fi fi
readarray -t lines <<<"$lines" readarray -t lines <<<"$lines"
for i in "${lines[@]}"; do for i in "${lines[@]}"; do
sed "${i}q;d" words.txt | tr '[:upper:]' '[:lower:]' sed "${i}q;d" $dictionary | tr '[:upper:]' '[:lower:]'
done done

6
sorter.sh Executable file
View file

@ -0,0 +1,6 @@
#!/bin/bash
inputfile=$1
rm "sorted$inputfile" &>/dev/null
while IFS= read -r line; do
echo $(printf "%s\n" "$line" | sed -- 's/./\0\n/g' | sort | tr -d -- "\n") >> sorted$inputfile
done < $inputfile