mirror of
https://github.com/G2-Games/anagram-finder.git
synced 2025-04-19 03:12:52 -05:00
Updated, fixed issues, added new sorting script
This commit is contained in:
parent
577143b1d4
commit
c75c81588b
2 changed files with 15 additions and 3 deletions
12
anagram.sh
12
anagram.sh
|
@ -1,16 +1,22 @@
|
|||
#!/bin/bash
|
||||
word=$(echo $1 | xargs)
|
||||
dictionary="${2:-words.txt}"
|
||||
if [[ $word = "" ]]; then
|
||||
echo "usage: ./anagram.sh <word>"
|
||||
echo "usage: ./anagram.sh <word> <dictionary file, optional>"
|
||||
exit 1
|
||||
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")
|
||||
lines=$(grep -xin -- "$wordsorted" sortedwords.txt | sed -e 's/:.*//g')
|
||||
lines=$(grep -xin -- "$wordsorted" sorted$dictionary | sed -e 's/:.*//g')
|
||||
if [[ $lines = "" ]]; then
|
||||
echo "No matches."
|
||||
exit 0
|
||||
fi
|
||||
readarray -t lines <<<"$lines"
|
||||
for i in "${lines[@]}"; do
|
||||
sed "${i}q;d" words.txt | tr '[:upper:]' '[:lower:]'
|
||||
sed "${i}q;d" $dictionary | tr '[:upper:]' '[:lower:]'
|
||||
done
|
||||
|
|
6
sorter.sh
Executable file
6
sorter.sh
Executable 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
|
Loading…
Reference in a new issue