Redirect script output to file after grep

i have simple program that generate log file 1 line every sec, i need to do grep for specific record then redirect to another file.

#!/bin/bash 
for i in `seq 1 20`;       
  do      
    echo $i            
    sleep 1
done  
./test.sh |egrep "5|10|15"
5
10
15

r

./test.sh |egrep "5|10|15" >> test.txt

test.txt always empty before the test.sh terminted.

on the real software the script is never terminated , is it possible to redirect the output before test.sh command process terminated?

sorry for my bad english

#!/bin/bash
for i in `seq 1 20`;
  do
    echo $i | egrep "5|10|15" > test.txt
    sleep 1
done

Your program already shows output before it terminates. So I strongly suspect it's not actually what you're actually doing. You are running some other program, something which does not flush its output.

How to force it to flush its output, depends on what it is.

Please show what you're actually doing, word for word, letter for letter, keystroke for keystroke.