简体   繁体   中英

sys.stdout.write does not grep

I have a python script that writes to a screen as a startup script. I want to monitor the progress of the server so I go into a loop checking the status. I don't want to write down the screen. Therefore I'm using sys.stdout.write. The issue is that the program I'm using (wlst Weblogic) prints all this logging which I don't want to display. I get around this by using grep and only printing lines that start with a space.

I have this function

def printRoll():
   print ' Is my Print working'
   myTest = ' Hello'
   myTest1 = 'test 123'
   sys.stdout.write("\r %s " % myTest)
   sys.stdout.flush()
   systime.sleep(10)
   sys.stdout.write("\r  %s " % myTest1)
   sys.stdout.flush()
   systime.sleep(10)

What I want to display is:

 Is my Print working
 Hello

then after 10 seconds Hello is over written with

 test 123

if I run the grep command

python 'printRoll()' | grep '^[[:space:]]\{1\}[A-Za-z0-9]' --line-buffered 

it only prints the first line

Is my Print working

It wont print any of the sys.stdout.write lines. I think it is an issue with the buffering but I can't find an example for my issue.

The python script works as expected but it also includes lots of other info I don't want to print to screen.

Any help appreciated.

I believe it is an old version of Python, ie 2.7

cheers James

The thing is the other lines aren't new lines as far as grep is concerned, as you are not outputting a \\n newline character.

What grep sees is – this output is via xxd for a hex dump so we can see all the characters – the following. 0a (on the second line in the dump) is the newline character. You can see that there are no more newline characters ( 0a s) after that, so there's nothing more for grep to slice the input to lines on.

00000000: 2049 7320 6d79 2050 7269 6e74 2077 6f72   Is my Print wor
00000010: 6b69 6e67 0a0d 2020 4865 6c6c 6f20 0d20  king..  Hello . 
00000020: 2074 6573 7420 3132 3320                  test 123 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM