Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SHELL

sample bash script

#!/bin/bash
# Editor Amir Ammar 

function SOS {
	echo "Enter: number / files / names / stats <chapter-name> / search <word>/ quit"
}

#########################################A
FILE=$PWD'/'$1 						    #M
if test -f "$FILE" ; then 				#I
	FILE=''		  						#R
else									#A
	echo "File $1 not found"            #M
	exit                                #M
fi                                      #A
for x in  $(cat $(echo $PWD'/'$1)) ; do #R
	FILE=$PWD'/'$x                      #COPY    The Earth With Out Art is Hust 'EH'
	if test -f "$FILE"; then            #RIGHT
    	continue						#E
    else				                #D
    	echo File $x not found			#I
    	exit                            #T
fi                                      #O
done                                    #R
##########################################
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)")
# Options
case $sweet in 
	"number") ## Find the number of chapters in a given directory  ###############################
	 echo $(cat $(echo $PWD'/'$1) | wc -l)  "chapters"  
	;;
	"files") ## find the name of the 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") ## find the title of each chapter ###################################################

	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*) ## find the number of the worlds and the lines within a chapter #######################
	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*) ## search for a specific word within a chapter and print that amount #################
		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 ################################################################################
	exit
	;;
esac 
done 
 
PREVIOUS NEXT
Tagged: #sample #bash #script
ADD COMMENT
Topic
Name
4+4 =