Reputation: 793
I have created a script to search a string in a text file and outputs all the lines with that string:
#!/bin/sh
STRING=$1
FILE=dsat.txt
if grep -q $STRING $FILE;
then
echo -e "$(grep $STRING $FILE)\n"
else
echo "Not found"
exit 0
fi
I want to expand on this where i have multiple details in the text file such as
Word1 Word2 - Word3 Word4 Word5
Word1 Word6 Word3 Word7 Word8
And in the search i search for "Word1 Word3"
I want it to still output these lines as the search case matches this line.
Is this possible using a grep?
Update:
#!/bin/sh
FILE=text.txt
STRING='$1'
grep "$(sed 's/ */\\|/g' <<<"$1")" $FILE
So when i run the script i do script.sh Word1 Word2 but it seems to not search word2 as well Thanks
Upvotes: 1
Views: 2267
Reputation: 6759
Till what i understand your question, you mean to say matching multiple patterns?
If that's what you are asking,
grep 'A B\|A B C' file
Would print all lines with either A B
or A B C
.
|
is for this pattern | or this pattern
. It needs to be backslashed though.
For using without backslash you can supply -E
flag.
Update:
Let's say you have all the words stored in words
,
grep "$(sed 's/ */\\|/g' <<<"$words")" file
Upvotes: 2