Help to process line by line and assign value to variables

Hi, there
I have a file with tab and space as field separator. I need to assign values to variables then work with them, line by line. The code I wrote only works when each line has only one word. It doesn't work for the current situation.

for line in `cat file.txt`; do
    ID=`awk '{print $NF}' $line`
    START=`awk -F"\t" '{print $4}' $line`
    END=`awk -F"\t" '{print $5}' $line`
    ...more works with these three variables...
done

I also try something like this, still not working:

cat file.txt | while read line; do
    ID=`awk '{print $NF}' $line`
    START=`awk -F"\t" '{print $4}' $line`
    END=`awk -F"\t" '{print $5}' $line`
    ...more works with these three variables...
done

Can you please help me? Thanks!

cat file.txt
213 length:6407        abc      xyz       2         343       .       +       0        id 3290
213 length:6407        def      mno      453     3922     .       +       0        id 3291
......

the desire result (not printing them out, but assign to variables and then work with the variables) is:
for line 1, $ID=3290, $START=2, $END=343, and I will do a bunch of works with these variables before next line...
for line 2, $ID=3291, $START=453, $END=3922, and I will do a bunch of works with these variables before next line...
......

awk -F"\t " '{printf("ID=%s,START=%s,END=%s\n",$11,$5,$6);}' input_file
1 Like

Sorry, this is not what I want.
I need the values of current line be assigned to variables, work with these variables before next line, and so on...
I will edit my post to make it clear. Sorry for the confusion! Thanks for helping me!

You need to use an array. The following is a bash array example.
Arrays start counting at 0 (zero), so an array of three words has 3 elements: word1 word2 word3

word1 is 0, word2 is 1, word3 is 2....

while read myline
do
   # make every word
   declare -a ( $myline )  # works with tab or space by default

   start=${arr[4]}
   end=${arr[5]}
   id=${arr[[10]}

done < inputfile

reference the elements you want and assign them to variables. I used your example to assign the variables.

1 Like

Thanks, Jim
There might be a typo, but the follow code works:

while read line; do
   declare -a arr=($line)

   start=${arr[4]}
   end=${arr[5]}
   id=${arr[[10]}
   ...
done < inputfile