togethersword8
togethersword8

Reputation: 13

Trim output of running program

I have a program that when it runs, outputs hundreds of lines starting with "Info:" and a few lines that have useful output. To make things easier, I have created a simple python and bash combination script to emulate the issue I'm having:

wait_2sec.py:

import time

print("Hello!")
time.sleep(2)
print("Goodbye!")

I am attempting to trim my output by running:

 python wait_2sec.py | sed '/Goodbye/d'

However, sed does not output Hello! until after the python script has finished. I don't know whether the pipe waits until after the program is finished to begin running the sed command, or if the sed command is the hangup. I am open to using another command to trim output if sed does not work for this use-case.

Upvotes: 0

Views: 162

Answers (2)

Armali
Armali

Reputation: 19395

I don't know whether the pipe waits until after the program is finished to begin running the sed command, or if the sed command is the hangup.

It's actually neither of both, it's that python normally buffers its output (if not to a terminal) until the buffer is full, hence Moustapha's suggestion may work provided that unbuffer is installed. But you can simply use python's built-in option -u (Force the stdout and stderr streams to be unbuffered.) instead:

python -u wait_2sec.py | sed '/Goodbye/d'

Upvotes: 3

Moustapha Ramadan
Moustapha Ramadan

Reputation: 109

You can try to run your script using the following command:

unbuffer python wait_2sec.py | sed '/Goodbye/d'

Refrence: https://unix.stackexchange.com/a/200413

Upvotes: 0

Related Questions