来自不断更新的输出的grep'ing输出

时间:2022-04-06 03:14:28

I'm trying to write a simple script around Lame to customize the program for my specific uses. What I'd like to do is parse out just the percent completeness from the Lame output.

我正在尝试围绕Lame编写一个简单的脚本来为我的特定用途定制程序。我想做的是解析Lame输出的完整性百分比。

Here's what the line looks like now:

这是现在的线条:

./lame --nohist ~/Desktop/Driver.wav ~/Desktop/Driver.mp3 2>&1| egrep -o "\([0-9\%]+\)"

But that returns nothing. Here's what the output from Lame looks like:

但这没有任何回报。以下是Lame的输出结果:

    LAME 3.99 (alpha 1, Jun  4 2009 19:42:31) 32bits (http://www.mp3dev.org/)
    warning: alpha versions should be used for testing only
    Using polyphase lowpass filter, transition band: 16538 Hz - 17071 Hz
    Encoding /Users/jkubicek/Desktop/Driver.wav
        to /Users/jkubicek/Desktop/Driver.mp3
    Encoding as 44.1 kHz j-stereo MPEG-1 Layer III (11x) 128 kbps qval=3
    Frame          |  CPU time/estim | REAL time/estim | play/CPU |    ETA 
    1500/8765   (17%)|    0:02/    0:15|    0:03/    0:17|   14.654x|    0:14

The last line of code dynamically updates as the file is converted. When I copy/paste/echo/pipe this exact text into my grep it finds the 17% just fine, but when I run it for real, it finds zilch.

最后一行代码在转换文件时动态更新。当我复制/粘贴/回显/管道这个确切的文本到我的grep它找到17%就好了,但当我真正运行它时,它找到zilch。

Edit: When I throw the output from lame into a text file, here is what the results look like:

编辑:当我将lame的输出抛出到文本文件中时,结果如下所示:

lameout.txt http://nothing2fancy.com/images/forum_images/lameout.txt-20090621-095324.png

It looks like I could push the output to a temp file and read the percentage complete out of there, but that feels awkward, like there should be a more elegant way to do this.

看起来我可以将输出推送到临时文件并读取完成的百分比,但这感觉很尴尬,就像应该有更优雅的方式来做到这一点。

3 个解决方案

#1


I suspect you may not be able to do this. The percentage output will probably go to the terminal via curses (to permit in-place dynamic updating), and so there'll be a limited output via stdout.

我怀疑你可能无法做到这一点。百分比输出可能会通过curses进入终端(允许就地动态更新),因此通过stdout输出的数量有限。

It may be worth redirecting the output to a file and see what gets written there. i.e.

将输出重定向到文件并查看在那里写入的内容可能是值得的。即

lame > /tmp/lame.log

#2


lame is probably not outputting this information in the same way when not connected to a terminal. Try running your lame command with at the end "> output.txt" and look at what it's printing when attached to another process.

当没有连接到终端时,lame可能不会以相同的方式输出此信息。尝试在末尾“> output.txt”运行你的lame命令,并查看附加到另一个进程时打印的内容。

The other very likely possibility is that "17%" is never actually printing out. What probably is printing is:

另一个非常可能的可能性是“17%”实际上从未打印出来。印刷的可能性是:

%, move left, 1, move left, 2, move left 3, ... move left, move left, 1, 7, move left 8, etc.

%,向左移动,1,向左移动,向左移动2,向左移动3,向左移动,向左移动,向左移动1,7,向左移动8等。

#3


I ended up using NSScanner to parse the output. Each line from the NSTask was sent to this method:

我最终使用NSScanner来解析输出。 NSTask中的每一行都被发送到此方法:

- (NSNumber *)parseOutputString:(NSString *)output {
  NSScanner *scanner = [NSScanner scannerWithString:output];
  NSString *endString = @"% complete";
  NSInteger percentComplete;
  BOOL didFindNumber = NO;

  while (![scanner scanString:endString intoString:nil]) {
        [scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:nil];
        didFindNumber = [scanner scanInteger:&percentComplete];

        if ([scanner isAtEnd]) {
              didFindNumber = NO;
              break;
        }
  }

  if (didFindNumber) {
        return [NSNumber numberWithInteger:percentComplete];
  } else {
        return [NSNumber numberWithInt:0];
  }
}

#1


I suspect you may not be able to do this. The percentage output will probably go to the terminal via curses (to permit in-place dynamic updating), and so there'll be a limited output via stdout.

我怀疑你可能无法做到这一点。百分比输出可能会通过curses进入终端(允许就地动态更新),因此通过stdout输出的数量有限。

It may be worth redirecting the output to a file and see what gets written there. i.e.

将输出重定向到文件并查看在那里写入的内容可能是值得的。即

lame > /tmp/lame.log

#2


lame is probably not outputting this information in the same way when not connected to a terminal. Try running your lame command with at the end "> output.txt" and look at what it's printing when attached to another process.

当没有连接到终端时,lame可能不会以相同的方式输出此信息。尝试在末尾“> output.txt”运行你的lame命令,并查看附加到另一个进程时打印的内容。

The other very likely possibility is that "17%" is never actually printing out. What probably is printing is:

另一个非常可能的可能性是“17%”实际上从未打印出来。印刷的可能性是:

%, move left, 1, move left, 2, move left 3, ... move left, move left, 1, 7, move left 8, etc.

%,向左移动,1,向左移动,向左移动2,向左移动3,向左移动,向左移动,向左移动1,7,向左移动8等。

#3


I ended up using NSScanner to parse the output. Each line from the NSTask was sent to this method:

我最终使用NSScanner来解析输出。 NSTask中的每一行都被发送到此方法:

- (NSNumber *)parseOutputString:(NSString *)output {
  NSScanner *scanner = [NSScanner scannerWithString:output];
  NSString *endString = @"% complete";
  NSInteger percentComplete;
  BOOL didFindNumber = NO;

  while (![scanner scanString:endString intoString:nil]) {
        [scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:nil];
        didFindNumber = [scanner scanInteger:&percentComplete];

        if ([scanner isAtEnd]) {
              didFindNumber = NO;
              break;
        }
  }

  if (didFindNumber) {
        return [NSNumber numberWithInteger:percentComplete];
  } else {
        return [NSNumber numberWithInt:0];
  }
}