You don't need to be an 'investor' to invest in Singletrack: 6 days left: 95% of target - Find out more
I have a long log file containing temperatures that is updated every 10 mins - lines are formatted date/time/temp1/temp2.
For example: [i]06/12/2014 20:50:01 56.500 15.000[/i]
I can extract temp2 using tail -c6 but I want to extract temp1 and put it into a variable and not sure how. Well I do have one idea: I could use [i]tail -c13[/i] to get both temps and then [i]head -c6[/i] to get the first temp but it seems a bit clumsy. Would [i]len[/i] be any help?
Any thoughts?
Spilt()
awk is your friend.
$temp1=awk '{print $2}' <tempfile
$temp2=awk '{print $3}' <tempfile
although you could use cut as well.
edited to include execution quotes and filename. Not sure if you want to run it from a script or not.
edit: which didn;t work. 😉
Where are you trying to do this? In a shell script? Or in some Python?
I'd prefer to do it in Python. The latest data is added to the end of the data file (not the beginning) every 10 mins.
I'll check awk.
Use a bash array:
$ line='06/12/2014 20:50:01 56.500 15.000'
$ vars=($line)
$ echo ${vars[2]}
56.500
$ echo ${vars[3]}
15.000
Edit: Even easier in Python
line='06/12/2014 20:50:01 56.500 15.000'
temp1, temp2 = line.split()[-2:]
print temp1, temp2
Right so you need to grab the line using tail then.
Top of my head so syntax may need correcting.
$temp1=?tail -1 tempfile |awk '{print $2}'?
$temp2=?tail -1 tempfile |awk '{print $3}'?
entities are formatting badly there but you need an execute single quote after the = and at the end of the line
Thanks guys [thumbs up gif ... if there was one]
backticks don't work on here.
with that format, I'd just use awk in bash commandline or script.
@Samurai
Perhaps this?
[code]
temp1=$(tail -1 tempfile |awk '{print $2}')
[/code]
The "$" at the start looks like you've been hacking on perl too long....
And don't use backticks as you can't nest them.
Sorted - running a bash script:
echo $(tail -1 tempdata.dat |awk '{print $3}') > watertemp.dat
and then using watertemp.dat in an existing python script to create a php file that my phone gets the temperatures from with a json request and sends them to my pebble phone 🙂
Saturday night fun.... it was either that or watch crap TV.
thanks all
Pretty sure you missed a step:
Output the log file to your printer, allow the printed paper to fall onto a wooden table, the capture an image of it with a web cam, the feed the image into optical character recognition, and use that data to create a copy of the log file.
😆
(Or you know, just do some RegEx in Python)
Didn't think about that Graham.... just got a new printer as well!
[i]temp1=$(tail -1 tempfile |awk '{print $2}')
The "$" at the start looks like you've been hacking on perl too long....
[/i]
php. I've been building phishing pages for the last few weeks.