Register
It is currently Wed Oct 01, 2014 4:19 pm

inserting comma into a string at the last but one position


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Mon Feb 07, 2011 10:28 am   

Joined: Mon Feb 07, 2011 9:00 am
Posts: 2
hi bash-gurus :)

i am an absolute newbie in writing bash scripts.

i have a bluetooth temperature sensor, which is connected as a virtual serial device at /dev/rfcomm0. the data comes without a comma, eg. 214 for 21,4 degrees or -125 for minus 12,5 degrees.
after much reading and some useful examples found in the internet, i created the following small bash script :

Code:
#!/bin/bash

comdevice="/dev/rfcomm0"

stty -F $comdevice 19200 -echo
while sleep 60
do
inputvar=`head $comdevice --lines=1`
echo $inputvar
done;


this echoes the data, but i have absolutely no idea, how to insert a comma at the last but one position.
i found some code to insert at a specific position using sed, but as the data can be at minimum 2 digits for 6,8 degrees, 3 digits for 21,3 degrees or -4,5 degrees or even 4 digits for -13,7 degrees, i don't know, how to do this.

can someone please help me out here?

thanks

groovy


Top
 Profile  
 PostPosted: Mon Feb 07, 2011 3:16 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
Code:
echo $inputvar | awk '{ printf("%.1f\n", $1/10); }'

this will print a 'float' (decimal value) with 1 decimal behind the point of your value divided by 10.


Top
 Profile  
 PostPosted: Mon Feb 07, 2011 11:34 pm   

Joined: Mon Feb 07, 2011 9:00 am
Posts: 2
thanks, works great :)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP