for loop, calling and editing multiple files inside

hey guys,

I'm trying to call and modify multiple files inside the for loop, i can't get it to work...

------------------------

AFILE=/dir/a_file.txt
BFILE=/dir/b_file.txt
CFILE=/dir/c_file.txt

ADESTFILE=/dir/a_dest_file.txt
BDESTFILE=/dir/b_dest_file.txt
CDESTFILE=/dir/c_dest_file.txt

AFINFILE=/dir/a_final_file.txt
BFINFILE=/dir/b_final_file.txt
CFINFILE=/dir/c_final_file.txt

for i in A B C
do
grep "something" $iFILE > $iDESTFILE
'do something with' $iFINFILE
done

-------------------------------

So as you guys can see, if I had A through Z, I don't want to write out everything in the loop 26 times.

Any ideas?

I tried '$"$i"FILE' but that didn't work. I just want to replace the $iFILE with AFILE and have it reference that file instead of the literal AFILE text.

thanks in advance.

are you going to define all your files (from A to Z) inside the script?? ie hardcoded? or are you going to store them in a file or something. then open the file and loop through them.?

Your problem is the shells way of parsing its input and what you have encountered is not a bug, its a feature.

You might want to read this thread where i have explained to problem and its solution in detail.

Still, in your case you would not need all the variables, because you could simply use:

for i in A B C ; do
     do_something /dir/${i}_file.txt > /dir/${i}_dest_file.txt
done

I hope this helps.

bakunin

The shell is interpreting iFILE as a variable name; enclose the variable name in braces to remove the ambiguity and eval for the indirection:

eval "grep 'something' \"\$${i}FILE\" > \"\$${i}DESTFILE\""

thank you ghostdog, bakunin and cfajohnson...between the three of you, i've got it taken cared of...

i'm going to take ghostdog and bakunin's suggestion of referenceing the file directly...i forgot to mention that there's a set of variables that is not as pretty looking like a_file.txt, b_file.txt, etc..., for that case, i'm going to use bakunin and cfa's eval statement...

thanks a bunch guys!

crap...i thought i had it but i forgot to add a small detail...please see if you guys can help me with this...

this relates to cfa's statement:

eval "grep 'something' \"\$${i}FILE\" > \"\$${i}DESTFILE\""

I'm trying to add these awk statements in the middle:

eval "grep 'something' \"\$${i}FILE\" | awk -F"=" '{print$2}' | awk -F" " '{print $1}' > \"\$${i}DESTFILE\""

it's not working for me...thanks

Don't try to cram everything into a single line. Generate the filenames first, then use a normal command (and you don't need awk).

eval "IFILE=\$${i}FILE DFILE=\$${i}DESTFILE"
result=$( grep 'something' "$IFILE" )
result=${result#*=}
printf "%s\n" "${result%% *}" > "$DFILE"