免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5659 | 回复: 11
打印 上一主题 下一主题

利用shell来获取某个程序实时输出数据的结果 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-01-05 16:49 |只看该作者 |倒序浏览
mplayer 扫描电视信号来获取频道列表,其输出数据为:
Audio: no sound
Starting playback...
Trying: 2 (57.75).
Trying: 3 (65.75).
Trying: 4 (77.25).
Trying: 5 (85.25).
Trying: 6 (112.25).
Trying: 7 (120.25).
Found new channel: 7 (#1).
Trying: 8 (128.25).
Found new channel: 8 (#2).
Trying: 9 (136.25).
Found new channel: 9 (#3).
Trying: 10 (144.25).
Found new channel: 10 (#4).
Trying: 11 (152.25).
Found new channel: 11 (#5).
Trying: 12 (160.25).
Found new channel: 12 (#6).
Trying: 13 (168.25).
Found new channel: 13 (#7).
Trying: 14 (176.25).
Found new channel: 14 (#.
。。。
Trying: 92 (839.25).
Trying: 93 (847.25).
Trying: 94 (855.25).
TV scan end. Found 14 new channels.
channels=7-ch1,8-ch2,9-ch3,10-ch4,11-ch5,12-ch6,13-ch7,14-ch8,15-ch9,16-ch10,17-ch11,18-ch12,19-ch13,20-ch14v4l2: 1154 frames successfully processed, 59 fram
es dropped.

Exiting... ()

问题是:需要实时统计所找到的channes 数量和 扫描总的channel数量(上述总共扫描了93个channels)
                比如
               mplayer 输出数据中出现“Trying: 5 (85.25). ”,此时统计所找到CHANNELS数(0个)和此时已经扫描的频道总数(4个)    ;
                。。。。。
               mplayer 输出数据中出现“Found new channel: 7 (#1). ”,此时统计所找到CHANNELS数(1个)和此时已经扫描的频道总数(6个)    ;
                依次类推

              每当mplayer输出一行数据,就需要统计以上两个参数,直到mpplayer程序exit !

欢迎各位提供一些参考意见和思路,非常感谢!

论坛徽章:
0
2 [报告]
发表于 2009-01-05 16:58 |只看该作者
while : ; do
    awk '/Trying/{try++};/Found/{found++}END{print try, found}' data
    sleep 2
done

论坛徽章:
0
3 [报告]
发表于 2009-01-05 17:09 |只看该作者
原帖由 WinnerBoy 于 2009-1-5 16:58 发表
while : ; do
    awk '/Trying/{try++};/Found/{found++}END{print try, found}' data
    sleep 2
done


lz的需求不是这个意思

论坛徽章:
0
4 [报告]
发表于 2009-01-05 17:15 |只看该作者
是trying一个,总数加一

new一个,找到的数加一

不过这个实时的话,好像只能使用shell循环了,不能是awk、sed之类的了

tail -f filename|while
...

论坛徽章:
0
5 [报告]
发表于 2009-01-05 17:34 |只看该作者
原帖由 welcome008 于 2009-1-5 17:15 发表
是trying一个,总数加一

new一个,找到的数加一

不过这个实时的话,好像只能使用shell循环了,不能是awk、sed之类的了

tail -f filename|while
...


我使用
tvplayer -vo null -tv driver=v4l2:norm=PAL tv:// -tvscan autostart  -nosound -quiet |
  awk '/Trying:/{ count++ ;print $count}'
结果不理想

[ 本帖最后由 ypxns 于 2009-1-5 17:36 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2009-01-05 19:36 |只看该作者
目前的办法是将mplayer输出的结果重定向到一个文件,然后每隔几秒钟去读这个文件,再使用awk来统计数据了!
  

以上写在一个脚本可能不行吧!
tvplayer -vo null -tv driver=v4l2:norm=PAL tv:// -tvscan autostart  -nosound -quiet > test
while :
do
awk '/Trying:/ { count++;print $count }'
done <test

tvplayer程序运行完成之后才能执行while程序,但是这样就反应不出实时获取数据的效果了,不知道各位有什么好的想法!

[ 本帖最后由 ypxns 于 2009-1-6 12:04 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2009-01-05 20:09 |只看该作者
原帖由 ypxns 于 2009-1-5 17:34 发表


我使用
tvplayer -vo null -tv driver=v4l2:norm=PAL tv:// -tvscan autostart  -nosound -quiet |
  awk '/Trying:/{ count++ ;print $count}'
结果不理想



怎么个不理想法?

论坛徽章:
0
8 [报告]
发表于 2009-01-06 11:55 |只看该作者
不好意思,现在才回复:
tvplayer -vo null -tv driver=v4l2:norm=PAL tv:// -tvscan autostart  -nosound -quiet | awk '/Trying:/{ num++ ;print $num}' 输出数据为:

Trying:
3
(85.25).

[ 本帖最后由 ypxns 于 2009-1-6 11:57 编辑 ]

论坛徽章:
0
9 [报告]
发表于 2009-01-06 13:37 |只看该作者
原帖由 ypxns 于 2009-1-6 11:55 发表
不好意思,现在才回复:
tvplayer -vo null -tv driver=v4l2:norm=PAL tv:// -tvscan autostart  -nosound -quiet | awk '/Trying:/{ num++ ;print $num}' 输出数据为:

Trying:
3
(85.25).



还有个85.25?

不应该啊

论坛徽章:
0
10 [报告]
发表于 2009-01-06 13:38 |只看该作者
看来还是整个while循环好了

awk一旦读完输出,就结束,管道关闭,不能一直显示了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP