Since it seems you only want to transform the "text representation", you can pipe the output of the command through sed
. The following command first removes ns
and then replaces s
by a .
:
~$ echo "2s576039936ns" | sed -e 's/ns//' -e 's/s/./'
2.576039936
Update
In a comment you stated that the nanosecond part is not zero-padded to exactly 9 digits. In that case, the output will be incorrect. To remedy, you can use the following awk
solution:
~$ echo "2s576039936ns" | awk -F'n?s' '{printf "%d.%09d\n",$1,$2}'
This will use either s
or ns
as field separator (multi-character field separators will be interpreted as regular expression, not merely as multi-character string), and print fields one and two (the latter now zero-padded to 9 decimals) with .
as separator.
So in your script, you could use
t=$( your_command | awk -F'n?s' '{printf "%d.%09d\n",$1,$2}' )
bash
only understands integers. But several other shells (ksh93
,zsh
,yash
,fish
) don't have that limitation.