Regex: Matching and extracting float number

Viewed 80

When I use grep to extract from status file using:

grep -ioP "vsim_nvclk_per_second.* " status

Output:

vsim_nvclk_per_second:float:27.2237552420673 |
vsim_post_munge_elapsed_s:float:16.1 |

But I just need

27.2237552420673

How do I change my regex ?

1 Answers

You can use

grep -ioP 'vsim_nvclk_per_second\D*\K\d[\d.]*' status

The vsim_nvclk_per_second\D*\K\d[\d.]* PCRE pattern (PCRE syntax is enabled with P option) matches

  • vsim_nvclk_per_second - a literal string
  • \D* - any zer or more non-digit chars
  • \K - match reset operator
  • \d[\d.]* - a digit and then any zero or more digits or dots.

See a PCRE regex demo.

Related