Skip to content

Instantly share code, notes, and snippets.

@nejni-marji
Created April 10, 2019 00:20
Show Gist options
  • Save nejni-marji/f87bb7cc4e1f5264f841860c7fb832dc to your computer and use it in GitHub Desktop.
Save nejni-marji/f87bb7cc4e1f5264f841860c7fb832dc to your computer and use it in GitHub Desktop.
grep for lines that contain multiple patterns in any order, using 2 passes of grep
#!/bin/zsh
re1="^($(
for i in "${@[@]}" ;
do echo -n "(?=.*($i).*\$)" ;
done
))"
re2="($(
echo "${(F)@}" | paste -s -d '|'
))"
# echo "re1=\"$re1\"" >&2
# echo "re2=\"$re2\"" >&2
# If this is run with argument `a b`, then:
# re1="^((?=.*(a).*$)(?=.*(b).*$))"
# re2="(a|b)"
< /dev/stdin | grep -P "$re1" | grep -P --color=auto "$re2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment