lundi 12 juin 2017

How to make search of words in several files via bash?

i have a script:

#!/bin/bash

text="hulk hogan,dolph ziggler"
IFS=","
word=( $text )

line=`ls workdir/*.txt`

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

It works, when i am trying to find hulk hogan,dolph ziggler. but if in the case, if these words are exists in all files. in the case, where hulk hogan is exists in first.txt but it does not exists in second.txt it will return error. Where i have error, help please. I want to search these words in all files. In the case, if some word from array does not exists in all files, i want to get error. Only in that case. But if word is exists in one file, and other word is exists in other file, i want to get positive result. Thanks for your attention.

Aucun commentaire:

Enregistrer un commentaire