(BASH) Using a loop variable to grep something in a file?

Hi,

I have a loop running until a variable L that is read previously in the full script. I'd like to grep some information in an input file at a line that contains the value of the loop parameter $i.
I've tried to use grep, but the problem is nothing is written in the FILE files. It seems grep cannot find "$i".
(I've tried to avoid awk to keep the sed etc I'm using after.)
Any idea?

Thanks in advance!

#!/bin/bash
L=5 
INPUT=input

for i in $(eval echo {0..$L}) 
do
    FILE=File$i.dat
    test -e $FILE || touch $FILE
    grep "#stuff 1 1 $i stuff_i_want_to_get"   $INPUT | sed "y/(),/   /" | cut -d' ' -f6-  >> $FILE
done

Using $i should be fine. I suspect something else isn't matching.

bash-3.2$ cat infile
#text1
#text2
#text3
#text4
#text5
bash-3.2$ for i in {2..4}; do grep "#text$i" infile; done
#text2
#text3
#text4
bash-3.2$

Do you have some example input?

What happens when you grep for the string from the command line? Do you get any output?

It would help a great lot if you posted your input file, and would be more specific about what is it you are hunting there.

Your $(eval echo {1..$L}) construct is odd. Use seq(1) instead, like

for i in `seq 0 $L` ; do ...

or use a while loop:

L=-1
while [ $((++L)) -lt 6 ] ; do ...

This line is not needed:

 test -e $FILE || touch $FILE

since the >> operator will create the file if it does not exist.

Please post sample input and the string you are trying to grep for.

Thanks for your help,

I've tried to grep only "#Stuff 1 1" and it works fine: I get a file with all the stuff_i_want_to_get. But I'd like to have L files...

Sorry, the input file is huge...
Here is one example line (with $i=9)

#OpNN 1 1 9 29 29 (0.31464941524,0.00358922141008)

I'm copying 29 , 0.31464941524 and 0.00358922141008 in three columns of the File9.dat.

What's your actual grep command?

Is the "#OpNN" constant on all lines?
If you do a loop with

grep "#OpNN 1 1 $i" >> file${i}.out

what do you get?