Extract number from a line with awk/sed

后端 未结 2 1561
梦如初夏
梦如初夏 2021-02-11 01:26

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

相关标签:
2条回答
  • 2021-02-11 01:57

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

    sed -r 's/.+ (\S+) tbr,.+/\1/'
    
    0 讨论(0)
  • 2021-02-11 02:08

    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
    
    0 讨论(0)
提交回复
热议问题