Patrick
Patrick

Reputation: 313

grep: how to get number between two pieces of text

I want to grep my cpu temp.

When i type in: ipmi-sensors -s 2352 I get: 2352: CPU Temp (Temperature): 30.00 C (NA/81): [OK]

I would like to grep the number between "(Temperature):" and "C"

Upvotes: 0

Views: 179

Answers (2)

Kalanidhi
Kalanidhi

Reputation: 5092

Try this way also

ipmi-sensors -s 2352 | awk -F'[: ]' '{print $7}'

sed version

ipmi-sensors -s 2352 | sed 's/[^)]\+): \([^ ]\+\).*/\1/'

Output:

30.00

Upvotes: 1

anubhava
anubhava

Reputation: 786081

Using grep -oP:

ipmi-sensors -s 2352 | grep -oP '\(Temperature\):\s*\K[\d.]+'
30.00

If your grep doesn't support -P option then use this awk:

ipmi-sensors -s 2352 | awk -F '.*\\(Temperature\\): *| C .*' '{print $2}'
30.00

Upvotes: 2

Related Questions