Using grep "val" index.php I get the list

<td class=val>   7.6</td>
<td class=val>  58</td>
<td class=val>1013.8 </td>
<td class=val> 1020 </td>
<td class=val>   0.2</td>
<td class=val>   2.4</td>

I'd like to filter and get only the value of the first td, that is, 7.6 and save it to use later with echo.

That value could change, so grep "7.6" is not good.

(!) The line in php containing that tag is line 42. A solution without this information could be better since the line number could change. But for a while, using its number can be a temporary solution.

I searched for a solution but I only found complex ones.

  • 2
    grep -m1 "=val" index.php | grep -o "[0-9.]*"
    – Costas
    Apr 18 '15 at 11:48
  • 1
    @Costas, thanks so much. It works perfectly. If you wish, please, post it as answer.
    – Sigur
    Apr 18 '15 at 11:53
  • @Costas, sometimes the number I want to get is negative -7.6 for example. And in this case, the grep returns empty. How to grep for the number (and its signal it there is one)?
    – Sigur
    Jan 14 '16 at 10:42
  • 1
    grep -wo "\-\?[0-9.]\+"
    – Costas
    Jan 14 '16 at 14:32

Using (gnu)grep:

grep -m1 -oP '(?<=class=val>).*?(?=</td>)'

grep -m1 -oP 'class=val>\s*\K[0-9.]*'        # \cite{Costas)
  • 1
    grep -m1 -oP 'class=val>\s*\K[0-9.]*'
    – Costas
    Apr 18 '15 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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