This bash script has a simple premise for now, recursively look through a directory, take every file from that directory, and from those files look for patterns that match email addresses, take those email addresses, sort them and then count the amount of times they appear, and sort them again.
Take those sorted emails and then with the head script, print the top emails according to the variable PUNISHED.
The directory is via the second argument, and if no directory is chosen then it simply runs through the current directory.
like this.
./myscript 20 /usr/home/AlabasterTenRing
Here is the code.
#!/bin/bash
shopt -s globstar
PUNISHED=$1
VENOM= $2
echo >> topemails.txt
for files in ./${VENOM}/**/*; do
if [ -f "${files}" ] ; then
<"$files" tr '[[:upper:]]' '[[:lower:]]' \
| grep -i -o '[A-Za-z0-9._%+-]\ + @[A-Za-z0-9.]\ + .[A-Za-z]\{2,4\}' \
| xargs -n 1 \
| sort \
| uniq -c \
| sort -nr > topemails.txt
fi
done
echo "The top \"${PUNISHED}\" emails are"
head -$PUNISHED topemails.txt
What ends up happening instead is that topemails.txt prints just as intended, but the number '1' is all that appears in it.
What could I do differently?