Powerful regular expression pattern searching.
If you want to search for multiple conditions then you can use OR
statements within the regular expression/pattern. There are two approaches, either escape the OR symbol, i.e. \|
or use the -E
flag.
# These two are equivalent grep -E 'a|b' my_file.csv grep 'a\|b' my_file.csv
If you have multiple conditions or terms you wish to be searched for using grep you can use the -f
flag to pass a file of terms to search for.
# Search for terms in 'file.txt' in files ending in *.py grep -f file.txt *.py
Here is one solution when you want to chain searches on the output of previous searchesuses recursion which Bash does support and is rarely used, but when it is can be incredibly powerful.
function chained-grep { local pattern="$1" if [[ -z "$pattern" ]]; then cat return fi shift grep -- "$pattern" | chained-grep "$@" }