how to extract substring and numbers only using grep/sed

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

    You can use:

    grep -P "miss rate \d+(\.\d+)?" file.txt
    

    or:

    grep -E "miss rate [0-9]+(\.[0-9]+)?"
    

    Both of those commands will print out miss rate 0.21. If you want to extract the number only, why not use Perl, Sed or Awk?

    If you really want to avoid those, maybe this will work?

    grep -E "miss rate [0-9]+(\.[0-9]+)?" g | xargs basename | tail -n 1
    

提交回复
热议问题