Extract number from a line with awk/sed

2020-06-06 01:47发布

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. 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.

2条回答
放我归山
2楼-- · 2020-06-06 02:19

Your current sed command would work well if you tweak the regex a bit:

sed -r 's/.+ (\S+) tbr,.+/\1/'
查看更多
叼着烟拽天下
3楼-- · 2020-06-06 02:31

Here is one approach with 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. The script looks at each record and prints the first field of any record that matches tbr.


A GNU grep approach that uses positive lookahead:

$ grep -Po '[0-9.]+(?= tbr)' file
25
29.97
查看更多
登录 后发表回答