I have exported some MS Word documents to plain text and use this function to parse the contents of the .txt
files in the current directory:
mo1 () {
for i in *.txt; do
echo "File: $i"
grep -n -HC 1 "$@" "$i"
done
}
If I had more than one pattern to look for, I could do mo1 | grep pattern2
. But what if I want to do something the result of which would be like grep -E 'pattern1.*pattern2[.*...]...'
depending on how many patterns are supplied to the function at runtime i.e. mo1 pattern1 pattern2 [...]
etc.? I can see the @
array could provide the number of items and I could construct over a loop a variable (finalpattern='$1.*$2.*$3') that would end up being the expression used for grep
. But I can't think of how to abstract that bit where you craft the expression in the function? Or is there a better/simpler way to do something like this?