Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Linux output redirection to file

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

./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

SAMPLE DEMO

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading