I could search lines that contain two specific words using the following in multiple files:
for f in $(ls *.txt); do echo ">"$f; more $f | grep word1 | grep word2 done
Now, I want to write a shell script to accomplish this so that I don't have to retype the whole thing again and again. I also wanted to add an option of using either one word or two words in the search. I will be passing these searched word to the shell script at the command line. I wrote the following shell script searchwords.sh:
word1=$1 word2=$2 for f in $(ls *.txt); do echo ">"$f ###if word2 is empty; then execute if [-z $word2]; then more $f | grep $word1 else ###if word2 is not empty more $f | grep $word1 | grep $word2 fi done
sh searchwords.sh word1 word2
Although I got what I want, I am also getting an error ouput
[-z: not found
I know my code is not right, but I cannot place it.