0

I'm trying to extract numbers from string using

cat out.log | tr -dc '0-9'

In Bash but I want to keep the decimals but this script will only keep numbers and no decimals

E_net4
  • 27,810
  • 13
  • 101
  • 139
Exeon
  • 3
  • 3
  • 1
    Does this answer your question? [How to extract numbers from a string?](https://stackoverflow.com/questions/17883661/how-to-extract-numbers-from-a-string) – bhristov May 21 '20 at 02:19
  • This is not what i want as it also deletes decimals – Exeon May 21 '20 at 02:31
  • Please add sample input (no descriptions, no images, no links) and your desired output for that sample input to your question (no comment). – Cyrus May 21 '20 at 04:40

2 Answers2

2

You need to add . and likely a space to the character class like so:

$ echo "12.32foo 44.2 bar" | tr -dc '[. [:digit:]]'
12.32 44.2 
SiegeX
  • 135,741
  • 24
  • 144
  • 154
1
grep -Eo '[[:digit:]]+([.][[:digit:]]+)?' <out.log

-o takes care that only the parts matching the pattern are written to stdout. The pattern greedily matches one or more digits, followed by optionally a decimal point and more digits. Note that this on purpose skips "numbers" such as .56 and 14., since they are considered malformed. If you want to include them, you can easily adjust the pattern to this.

user1934428
  • 19,864
  • 7
  • 42
  • 87
  • 1
    Works well with input `echo "Number 1 is odd. Number 2 is even. Just like 4. And 3.45 is a fractal.` – Walter A May 21 '20 at 08:15