How to use while loop in bash shell to read a file with 4 lines of gap

Hi ,

I am currently using the while loop in bash shell, as follows.

while read line
do
echo $line
done < file.txt

However, i want to use the while loop on file.txt, which will read the file with 4 lines of gap.
Ex- if file.txt is a file of 100 lines, then i want to use the loop such that, it will read the file with n-lines of gap. where, n=1,2,3,4, ....

This is a bit urgent. Please help me.

Thanks and Regards,

Jitendriya Dash.

Use modulus

i=1;n=3  # n is your gap number
while read line
do if [ "$((i % n ))" -eq 0 ] 
then 
echo $line; 
fi;
i=$(( i + 1 ));
done< file.txt

cheers,
Devaraj Takhellambam

awk 'NR%4==0' file

Thanks a lot. I think, the first suggestion will help me.

Thanks and Regards,

Jitendriya Dash