Friday, July 22, 2022

[SOLVED] How cut one character after the dot in a shell script variables

Issue

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


Solution

If you are using Bash:

$ var=23.123
$ [[ $var =~ [0-9]*(\.[0-9]{,1})? ]] && echo ${BASH_REMATCH[0]}

Output:

23.1


Answered By - James Brown
Answer Checked By - Marilyn (WPSolving Volunteer)