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
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
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
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.