简体   繁体   English

使用awk / sed从一行中提取数字

[英]Extract number from a line with awk/sed

I have this string: 我有这个字符串:

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

and I would like to extract 25 from it. 我想从中提取25 I use: 我用:

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

and it returns what I need. 它返回我需要的东西。

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. 它不会再回归我需要的了。

I tried different alternate ways so the value for tbr is returned in both cases but couldn't find the right expression. 我尝试了不同的替代方法,因此在两种情况下返回tbr的值,但找不到正确的表达式。

Here is one approach with awk : 这是awk一种方法:

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

Explanation: 说明:

By default awk treats each line as a record, By setting RS to , we set the record separator to a comma. 默认情况下, awk将每一行视为记录,通过将RS设置为,我们将记录分隔符设置为逗号。 The script looks at each record and prints the first field of any record that matches tbr . 该脚本查看每条记录并打印与tbr匹配的任何记录的第一个字段。


A GNU grep approach that uses positive lookahead : 使用正向前瞻的 GNU grep方法:

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

如果你稍微调整一下正则表达式,你当前的sed命令会运行良好:

sed -r 's/.+ (\S+) tbr,.+/\1/'

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

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