how to extract substring and numbers only using grep/sed

后端 未结 6 793
悲哀的现实
悲哀的现实 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:33

    The grep-and-cut solution would look like:

    to get the 3rd field for every successful grep use:

    grep "^miss rate " yourfile | cut -d ' ' -f 3
    

    or to get the 3rd field and the rest use:

    grep "^miss rate " yourfile | cut -d ' ' -f 3-
    

    Or if you use bash and "miss rate" only occurs once in your file you can also just do:

    a=( $(grep -m 1 "miss rate" yourfile) )
    echo ${a[2]}
    

    where ${a[2]} is your result.

    If "miss rate" occurs more then once you can loop over the grep output reading only what you need. (in bash)

提交回复
热议问题