how to extract substring and numbers only using grep/sed

后端 未结 6 869
悲哀的现实
悲哀的现实 2021-02-09 14:29

I have a text file containing both text and numbers, I want to use grep to extract only the numbers I need for example, given a file as follow:

miss rate 0.21          


        
6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-09 14:42

    If you really want to use only grep for this, then you can try:

    grep "miss rate" file | grep -oe '\([0-9.]*\)'
    

    It will first find the line that matches, and then only output the digits.

    Sed might be a bit more readable, though:

    sed -n 's#miss rate ##p' file
    

提交回复
热议问题