Answer the question
In order to leave comments, you need to log in
How to find multiple words in multiple files using BASH?
There is this script:
#!/bin/bash
text="hulk hogan,dolph ziggler"
IFS=","
word=( $text )
line=`ls workdir/*.txt`
unset IFS
for a in "${word[@]}"; do
for m in $line; do
if grep -q "$a" "$m"; then
echo "$a word is exists"
grep "$a" "$m"
else
echo "$a word does not exists"
exit 1
fi
done
done
hulk hogan,dolph ziggler
in multiple files. If they are, the script will skip, saying that the line such and such is in the file such and such. But it looks for the whole of these two words in each file. That is, if it does not find at least one of the words in one file, it will exit the script body. I want it to throw an exception if one of the words is not in all files. And if, for example hulk hogan
, there is in the first.txt file, and dolph ziggler
there is in second.txt, then the script should work, and not leave its body. Answer the question
In order to leave comments, you need to log in
In general, this is done in one line using find.
The output is a list of files containing any of the searched strings.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question