Created
April 10, 2019 00:20
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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