Hi.
Here are a few alternatives; One is the use of shell expressions, the other is a relative of grep, agrep
:
#!/usr/bin/env ksh
#!/usr/bin/env bash
#!/usr/bin/env zsh
# @(#) s1 Demonstrate match multiple strings AND and OR, agrep, shell.
# Utility functions: print-as-echo, print-line-with-visual-space.
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && . $C dixf agrep
FILE=${1-data1}
pl " Data file $FILE:"
cat $FILE
pl " With agrep, \"and\":a b c :"
agrep "a;b;c" $FILE
pl " With agrep, \"or\":a b c :"
agrep "a,b,c" $FILE
# A pattern-list is a list of one or more patterns
# separated from each other with a & or |. A & signifies
# that all patterns must be matched whereas | requires
# that only one pattern be matched.
# shopt -s extglob
# shopt extglob
pl " With shell expressions, \"and\", a b c ksh (bash, zsh fail):"
while read line
do
if [[ $line == @(*a*&*b*&*c*) ]]
then
pe "Matched line: $line"
fi
done < $FILE
pl " With shell expressions, \"or\", a b c ksh (zsh fail):"
while read line
do
if [[ $line == @(*a*|*b*|*c*) ]]
then
pe "Matched line: $line"
fi
done < $FILE
pl " Some detail about agrep:"
dixf agrep
exit $?
producing:
$ ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution : Debian 8.7 (jessie)
ksh 93u+
dixf (local) 1.42
agrep - ( /usr/bin/agrep, 2012-04-12 )
-----
Data file data1:
also
abracadabra
chock full of beans
hello, world.
silent
-----
With agrep, "and":a b c :
abracadabra
chock full of beans
-----
With agrep, "or":a b c :
also
abracadabra
chock full of beans
-----
With shell expressions, "and", a b c ksh (bash, zsh fail):
Matched line: abracadabra
Matched line: chock full of beans
-----
With shell expressions, "or", a b c ksh (zsh fail):
Matched line: also
Matched line: abracadabra
Matched line: chock full of beans
-----
Some detail about agrep:
agrep search a file for a string or regular expression, with... (man)
Path : /usr/bin/agrep
Version : - ( /usr/bin/agrep, 2012-04-12 )
Type : ELF 64-bit LSB shared object, x86-64, version 1 (S ...)
Help : probably available with -h
Repo : Debian 8.7 (jessie)
The agrep
will be much faster for files of more than trivial size.
Best wishes ... cheers, drl