grep pattern1 | grep pattern2 | ...I would like to use single grep because I am building arguments dynamically, so everything has to fit in one string
This isIt's actually possible to build the pipeline dynamically (without resorting to eval):
# Executes: grep "$1" | grep "$2" | grep "$3" | ...
function chained-grep {
local pattern="$1"
if [[ -z "$pattern" ]]; then
cat
return
fi
shift
grep -- "$pattern" | chained-grep "$@"
}
cat something | chained-grep all patterns must match order but matter dont
It's probably not a very goodefficient solution but illustrates a somewhat cool "trick"though.
function chained-grep {
local pattern="$1"
if [[ -z "$pattern" ]]; then
cat
return
fi
shift
grep -- "$pattern" | chained-grep "$@"
}
cat something | chained-grep all patterns must match order but matter dont