#!/bin/bash
function SOS {
echo "Enter: number / files / names / stats <chapter-name> / search <word>/ quit"
}
FILE=$PWD'/'$1
if test -f "$FILE" ; then
FILE=''
else
echo "File $1 not found"
exit
fi
for x in $(cat $(echo $PWD'/'$1)) ; do
FILE=$PWD'/'$x
if test -f "$FILE"; then
continue
else
echo File $x not found
exit
fi
done
while SOS && read -s input ; do
sweet=$(echo $input | egrep "(number)|(files)|(names)|(stats)[s]*(chapter-[0-9]*[0-9]*)*|(search)[s]*[A-Za-z]*|(quit)")
case $sweet in
"number")
echo $(cat $(echo $PWD'/'$1) | wc -l) "chapters"
;;
"files")
wd=$PWD
for chapter in $(cat $(echo $PWD'/'$1)) ; do
echo $( cat $wd/$chapter | head -n1 ): $chapter >> temp1.txt
done
sort -V temp1.txt > temp2.txt
cat temp2.txt
rm temp2.txt
rm temp1.txt
;;
"names")
wd=$PWD
for chapter in $(cat $(echo $PWD'/'$1)) ; do
h="$( cat $wd/$chapter | head -n1 ): "
name="$( cat $wd/$chapter | head -n3 | tail -n1)"
full="$h$name"
echo $full >> temp1.txt
done
sort -V temp1.txt > temp2.txt
sed 's/$/ /g' temp2.txt
rm temp2.txt
rm temp1.txt
;;
stats*)
wd=$PWD
for chapter in $(cat $(echo $PWD'/'$1)) ; do
ch="$( cat $wd/$chapter | head -n1 ): "
line_words="$( cat $wd/$chapter | tail -n +3 |wc -l) lines, $( cat $wd/$chapter| tail -n +3 | wc -w) words"
full=$ch$line_words
echo "$full" >> temp1.txt
done
sort -V temp1.txt > temp2.txt
if [[ $sweet = "stats" ]] ; then
cat temp2.txt
else
if [[ $sweet =~ (stats)[s]*(chapter-[0-9]*[0-9]*)* ]] ; then
chap=($sweet)
grep -i "${chap[1]}" temp2.txt
fi
fi
rm temp2.txt
rm temp1.txt
;;
search*)
arr=($sweet)
wd=$PWD
touch unsorted.txt
touch sorted.txt
for chapter in $(cat $(echo $PWD'/'$1)); do
h="$(head -n1 $wd/$chapter )"
get=$(cat $wd/$chapter | egrep -w -i ${arr[1]} | wc -l)
full="$h: $get"
if [[ $get -gt 0 ]] ; then
echo $full >> unsorted.txt
fi
done
if [[ $(cat unsorted.txt | wc -l) -gt 0 ]] ; then
sort -V unsorted.txt > sorted.txt
cat sorted.txt
fi
rm unsorted.txt
rm sorted.txt
;;
"quit")
exit
;;
esac
done