Issue
This is in RHEL 7.
Simple shell script:
while [ 1 ] ;
do
echo "Test Message 1"
echo "Test Message 2"
echo ""
sleep 10
done
Following works fine:
./loop-test.sh | gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }'
[2023-12-21 10:04:47] Test Message 1
[2023-12-21 10:04:47] Test Message 2
I want to redirect output to a file. So, I did: ./loop-test.sh | gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }' > t.txt
It creates output file t.txt, but it is empty. I tried several redirections... including "tee" with no luck.
Is there a way I can capture the output in a file?
Solution
This is happening due to output buffering. When the output is redirected to a file, both gawk
and the shell
buffer the output before writing it to the file. To disable buffering in gawk
, you can use the fflush()
function. Something like:
./loop-test.sh | gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0; fflush() }' > t.txt
Answered By - mandy8055 Answer Checked By - Terry (WPSolving Volunteer)