When a variable is used in a test expression it's contents are treated as a regex, without the need for
/../ brackets. So I suggest modifying it like this:
Code:
for (( j=1; j <= $c_subl; j++ )) ;do
for (( k=1; k<=$c_comp; k++ )); do
pattern="species,subl,cmp=[[:blank:]]+$k[[:blank:]]+$j"
awk -v "ss=$search" 'ss { print }' "$i"
done
done
I agree with grail though that running multiple
awk's in a double loop is quite inefficient. It seems to me that it would be better to build a list of all possible matching patterns first, then use a simple "
grep -f" to extract them. For this I recommend a function and a
process substitution.
Code:
genlist(){
local cnt1 cnt2
for (( cnt1=1; cnt1<=$1; cnt1++ )); do
for (( cnt2=1; cnt2<=$2; cnt2++ )); do
echo "species,subl,cmp=[[:blank:]]+$cnt1[[:blank:]]+$cnt2"
done
done
}
grep -E -f <( genlist "$c_comp" "$c_subl" ) "$infile"
I also recommend using variable names that more clearly describe their function, BTW. And don't forget to always quote them!
Actually, come to think of it, we could probably just use this single
awk command instead:
Code:
awk -v "v1=$c_comp" -v "v2=$c_subl" '/species,subl,cmp/ && $2<=v1 && $3 <=v2 { print }' "$infile"