Getting value into a variable from file

Hi Guys ,
Need help with an issue i am facing . I have a text file test.txt with the following content

 
File: test.txt
------------
4 

From this file , i need to take value of 4 into a variable . I am doing it like this

 
COUNT =`cat test.txt`
echo "$COUNT"

But the variable is showing as null . Can you please help me in initializing the variable .

Thanks in Advance

Would this work for you:

$ COUNT=`tail -1 test.txt`
$ echo $COUNT
4
1 Like

Yes It helped ..
Thanks a lot :slight_smile:

---------- Post updated at 01:23 PM ---------- Previous update was at 01:18 PM ----------

Have a small doubt .
While using an iterator , i need to take the fields as seen as below .
But the Iterator is not been displayed . Can you please help with this .

ITERATOR=0
COUNT=`cat test.txt`
echo "$COUNT"
 
while [ $ITERATOR -lt $COUNT ]
do
   echo $ITERATOR
   echo "$COUNT"
   awk 'NR=="$ITERATOR"{print;exit}' EMAIL_REPORT.CSV >temp_mail.txt
   TO=`cat temp_mail.txt` 
ITERATOR=`expr $ITERATOR + 1`
done

By using set -x , i checked the data been sent .

+ awk NR=="$ITERATOR"{print;exit} EMAIL_REPORT.CSV

Can you please help with this .

Use

awk -v iter=$ITERATOR 'NR==iter{print;exit}' EMAIL_REPORT.CSV >temp_mail.txt

or

sed -n "$ITERATORp'  EMAIL_REPORT.CSV >temp_mail.txt
1 Like

Thanks a lot . The 2nd one worked prefectly for me

Not sure I understand your logics. You seem to want to put lines 1,2,3 from file EMAIL_REPORT.CSV into individual versions of temp_mail.txt. The first iteration of your loop is in vain, as NR will never assume 0, the value of your iterator. Still awk will read through your entire file!
Why don't you try sth like (on a recent bash/ksh shell)

 while ((++ITERATOR <= COUNT)); do read LINE; echo $LINE >temp_mail.txt; ...  done < EMAIL_REPORT.CSV