I got the required answer but surely there is a better way to write my query? grep -v " " mywords.txt | grep -E "(.*b.*d|.*d.*b)" | egrep "\\b[a-z]{