Part 1 [50 marks] Your goal is to develop a program that can be used to perform
ID: 3736007 • Letter: P
Question
Part 1 [50 marks]
Your goal is to develop a program that can be used to perform keyword searches in a text files in the local directory. Say you your local directory contains the following files (text files with extension .txt)
When we run your program as follows
The program will produce the following output
Note that dog.jpg was ignored, since it doesn't have .txt extension.
Part 2 [30 marks]
In the previous part, your program processes the database (meaning, the list of text files in the current folder) anew each time it is used to perform search. This clearly cannot scale. Can you think of ways where the program doesn't have to process the entire database everytime a search is performed?
So then
will process the database, saving the relevant info, and
will behave as before (except now it won't have to process the database from scratch every time a search is performed). Briefly describe your solution.
Is it any faster?
Programming suggestions
You are free to use all of STL for part 1 and part 2 of this assignment.
To those of you are feeling adventurous, I suggest that you look into Boost C++ for figuring out how to find the list of files in the current directory.
If Boost C++ library is not your cup of tea, you may want to look at the DIR structure available in dirent.h to find the list of files in a directory.
It is worthwhile to look at std::map structure for setting up indices for quick seach queries.
PLEASE POST THE CODE THANKS
Explanation / Answer
Explanation:
First of all, we have to find which all text files are present:
list = ls | grep "*.txt"
eval $list /* this gives you list of all the .txt files.
/* pass this an argument to awk script that perform the match operation
Then we will create an array keyword to store all the pattern being search
Now one by one each keyword is picked and searched in file.
NR will return the number of the current line
Code:
-exec awk -v
'BEGIN{
declare -keyword var
list = ls | grep "*.txt"
eval $list
for var in "$@"
do
keyword[i]=$1
shift
done
};
awk '!patterns_finished{ keywords[$0]; next}
{
for (p in keywords) if ($0 ~ p) {
position[p] = NR
count[p]++
}
}
END {
for (p in keywords)
printf "String found %s: at line %d in file %s ", position[p], keywords[p], list
}' $list patterns_finished=1 file