Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to grep the number 50.0 from the string temp=50.0'C

In JavaScript I can use the expression /temp=(.*?)'C/ to get it.

But I cant get it running in grep.

share|improve this question
50.0 isn't in the source string, how would you extract it in any language? – Bratchley May 16 '14 at 21:17
Sorry, my fault. Now it is. – user2765509 May 16 '14 at 21:19
up vote 4 down vote accepted

You can use lookbehinds and lookaheads after enabling PCRE (via -P):

root@xxxxxxvlp03 ~ $ echo "temp=50.0'C" | grep -Po "(?<=temp\=).*(?=\'C)"
root@xxxxxxvlp03 ~ $
share|improve this answer

Or you can use sed to extract a matching subgroup in a pretty similar way to your Javascript example:

echo "temp=50.0'C" | sed "s/temp=\([^']*\)'C/\1/"
share|improve this answer

Depends on what you know about the string. In this case you could define the wanted result as all the characters which are either a digit or a dot:

start cmd:> echo "temp=50.0'C" | grep -Eo '[0-9\.]+'
share|improve this answer

With sed you can combine conditional addressing and backreferences like this:

sed "/temp=\([0-9.]*\)'C/!d;s//=\n\1\n/;s/.*=\n//;P;D"

It is admittedly little more than an expansion on godlygeek's answer, but written with an entire file input in mind. It prints only the sequence of digits and .dots occurring between the two strings temp= and 'C - even if multiples occur on the same line - but each digit|dot sequence is printed on its own line.

Also note that for maximum portability you should replace the n's in \n\1\n with literal newlines if it gives you any trouble - though most sed's will support it as written - like:


Anyway, it enables you to handle the wacky stuff as portably as I know how it can be done. Like:

sed "/temp=\([0-9.]*\)'C/!d;s//=\n\1\n/;s/.*=\n//;P;D" <<\DATA

some nonsense temp=here

Its mechanism is fairly simple - it deletes lines not containing your target string. For those that do it selects only the portion you wish to keep and surrounds it on each side with newline characters - and it puts a character known not to be in your target before the first. Then it deletes as much of pattern space as it can while still ending on the arbitrary =\n separator.

Last it Prints only up to the first newline occurring in pattern space - which we just inserted immediately behind our target - and Deletes same before starting a new cycle with what remains.

share|improve this answer

this may seem helpful if you want just extract the number part of 50.0'C , you can also use cut. it's may seem simple than using regular expression.

echo "temp=50.0'C" | cut -d= -f2 | cut -d\' -f1

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.