I'm trying to write a scirpt for the bash shell. I'm given 4 parameters:
- path of a directory
- extension of a file
- a word
- a number
I have to look for all files and files of subdirectories of the path, for files with the given extension. Then, look in the files for the lines matching the word given, but only if the number of words in the line is bigger or equal to the number provided.
For example:
If localDirectory has: image.png script.sh text.txt.
And text.txt has:
This is a text file
It contains many words
This is an example for a simple text file
Then give the command: ./example.sh localDirectory txt text 7
I should output: This is an example for a simple text file.
For now, my script looks like this:
if [ "$#" -ne 4 ];
then
echo "Not enough parameters"
exit 1
fi
find $1 -name "*.$2" -exec grep -wi $3 {} \;
And it works just fine for the first 3 goals, I just don't know how to filter now the result such that the line has a greater or equal number of words as $4.
I could of course create a new file and output there, and then read from it and print only the relevant lines, but I would rather do this without creating new files.
I'm very new to shell scripting.
Any ideas?