time complexity - Recursive bash function vs iterative "eval" string building: Which performs better? -


i creating function make multiple grep's on every line of file. run following:

 cat file.txt | agrep string1 string2 ... stringn  

the idea print every line contains strings: string1, string2, ..., stringn, followed 2 approaches fist recursive method :

agrep () {     if [ $# = 0 ];         cat     else         pattern="$1"         shift         grep -e "$pattern" | agrep "$@"     fi } 

by other hand have second approach related iterative method, since using method :

function agrep () {     in $@;      cmd+=" | grep '$a'";   done ;   while read line ;     eval "echo "\'"$line"\'" $cmd";   done; } 

this 2 approaches works know if can tell me 1 more efficient? , if it's posible if there way measure in bash?, since consider don't have enough experience determine because don't know if bash it's programming language works better iterative methods or recursive methods or maybe if it's expensive use eval.

this 2 functions designed work large texts , process every line of texts, appreciate explanation or advice of this.

this example of text file called risk:

1960’s. until 1990’s purely theoretical analysis of problem of function estimation given collection of data. in middle of 1990’s new types of learning algorithms (called support vector machines) based on developed t 

and if run:

cat risk | agrep until 

i get:

1960.s. until 1990.s purely theoretical analysis of 

but other hand if run:

cat risk | agrep until new 

prints nothing since there inst line 2 strings, function designed clarify usage of function.

i agree comments , answers have informed of pitfalls of current approach.

based on suggestion made karakfa, suggest using function calls awk, along these lines:

agrep() {     awk 'begin {         # read command line arguments , unset them         (i = 1; < argc; ++i) {             strings[i] = argv[i]             argv[i] = ""         }     }     {         (i in strings) {             # if line not match, skip             if ($0 !~ strings[i]) next         }         # print remaining lines         print     }' "$@" } 

this passes in of arguments function arguments awk, treat them filenames. each argument added new array, strings , removed argv before lines of input processed.

use this:

agrep string1 string2 string3 < file 

Comments