从使用awk / sed的行数提取 [英] Extract number from a line with awk/sed

查看:162
本文介绍了从使用awk / sed的行数提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个字符串:

Stream #0:0: Video: vp6f, yuv420p, 852x478, 1638 kb/s, 25 tbr, 1k tbn, 1k tbc

和我想提取物 25 从它。
我使用的:

and I would like to extract 25 from it. I use:

sed -r 's/.+([0-9]{2} tbr).+/\1/'

和返回我需要什么。

无论如何,如果不是我遇到像

Anyway, if instead I encounter a string like

Stream #0:0(eng): Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 11981 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 30k tbc

它不会返回我需要什么了。

It won't return what I need anymore.

我尝试了不同的替代方式,因此该值用于 TBR 在两种情况下会返回,但找不到合适的前pression。

I tried different alternate ways so the value for tbr is returned in both cases but couldn't find the right expression.

推荐答案

下面是一个方法 AWK

$ awk '/tbr/{print $1}' RS=, file
25
29.97

说明:

在默认情况下 AWK 把每行一个记录,通过设定 RS 我们设置记录分隔符为逗号。该脚本着眼于每一个记录,并打印匹配 TBR

By default awk treats each line as a record, By setting RS to , we set the record separator to a comma. The script looks at each record and prints the first field of any record that matches tbr.

A 使用积极前瞻的grep GNU 办法

$ grep -Po '[0-9.]+(?= tbr)' file
25
29.97

这篇关于从使用awk / sed的行数提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆