I need to remove all characters after the first one after the dot:
example:
the temperature is 28.34567 C°
I need only 28.3
I've tried with cut -d'.' -f1
but cut all after the dot..
Thanks a lot
CodePudding user response:
You could add this line to your script if you have python3
python -c "print(f\"{28.34567:.1f}\")"
This solution rounds the result (ceil)
Output:
28.3
CodePudding user response:
There are a few ways to do this, each with its own issues. The trivial solution is sed
. Something like:
$ echo "the temperature is 28.37567 C°" | sed -e 's/\([[:digit:]]\.[0-9]\)[0-9]*/\1/g'
the temperature is 28.3 C°
but you probably don't want truncation. Rounding is probably more appropriate, in which case:
$ echo "the temperature is 28.37567 C°" | awk '{$4 = sprintf("%.1f", $4)}1'
the temperature is 28.4 C°
but that's pretty fragile in matching the 4th field. You could add a loop to check all the fields, but this gives the idea. Also note that awk
will squeeze all your whitespace.
CodePudding user response:
example:
#!/bin/sh
riscaldamento_in=$($path3/owread riscaldamento_in/temperature | sed 's/^[ \t]*//' | cut -d'.' -f1)```
now the variable 'riscaldamento_in' result it is 28
but I need one decimal..
a piece of advice...?
CodePudding user response:
If you are using Bash:
$ var=23.123
$ [[ $var =~ [0-9]*(\.[0-9]{,1})? ]] && echo ${BASH_REMATCH[0]}
Output:
23.1