简体   繁体   English

sys.stdout.write不grep

[英]sys.stdout.write does not grep

I have a python script that writes to a screen as a startup script. 我有一个Python脚本作为启动脚本写到屏幕上。 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. 因此,我正在使用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. 问题是我正在使用的程序(wlst Weblogic)打印所有我不想显示的日志。 I get around this by using grep and only printing lines that start with a space. 我通过使用grep和仅打印以空格开头的行来解决此问题。

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 然后10秒钟后,您好已写满

 test 123

if I run the grep command 如果我运行grep命令

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. 它不会打印任何sys.stdout.write行。 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. python脚本可以按预期工作,但是它还包含许多其他我不想打印到屏幕上的信息。

Any help appreciated. 任何帮助表示赞赏。

I believe it is an old version of Python, ie 2.7 我相信它是Python的旧版本,即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. 问题是其他的线都没有新线尽可能grep而言,既然你是不是输出\\n换行符。

What grep sees is – this output is via xxd for a hex dump so we can see all the characters – the following. grep看到的是–该输出通过xxd进行十六进制转储,因此我们可以看到所有字符–以下。 0a (on the second line in the dump) is the newline character. 0a (在转储的第二行)是换行符。 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. 您可以看到之后没有更多的换行符( 0a ),因此grep不再需要将输入切成行。

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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