Ishan Kumar
Ishan Kumar

Reputation: 33

Regex: Matching and extracting float number

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 ?

Upvotes: 2

Views: 208

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627488

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.

Upvotes: 1

Related Questions