Linux/python dudes ...
 

  You don't need to be an 'investor' to invest in Singletrack: 6 days left: 95% of target - Find out more

[Closed] Linux/python dudes - how do I extract this data?

13 Posts
7 Users
0 Reactions
112 Views
Posts: 13916
Free Member
Topic starter
 

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?


 
Posted : 06/12/2014 9:08 pm
Posts: 23277
Free Member
 

Spilt()


 
Posted : 06/12/2014 9:11 pm
Posts: 2
Free Member
 

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. 😉


 
Posted : 06/12/2014 9:12 pm
Posts: 31206
Full Member
 

Where are you trying to do this? In a shell script? Or in some Python?


 
Posted : 06/12/2014 9:16 pm
Posts: 13916
Free Member
Topic starter
 

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.


 
Posted : 06/12/2014 9:23 pm
 sas
Posts: 0
Free Member
 

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


 
Posted : 06/12/2014 9:24 pm
Posts: 2
Free Member
 

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


 
Posted : 06/12/2014 9:33 pm
Posts: 13916
Free Member
Topic starter
 

Thanks guys [thumbs up gif ... if there was one]


 
Posted : 06/12/2014 9:44 pm
Posts: 6194
Full Member
 

backticks don't work on here.

with that format, I'd just use awk in bash commandline or script.


 
Posted : 06/12/2014 9:46 pm
Posts: 7076
Full Member
 

@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.


 
Posted : 06/12/2014 10:02 pm
Posts: 13916
Free Member
Topic starter
 

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


 
Posted : 06/12/2014 10:51 pm
Posts: 31206
Full Member
 

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)


 
Posted : 06/12/2014 11:10 pm
Posts: 13916
Free Member
Topic starter
 

Didn't think about that Graham.... just got a new printer as well!


 
Posted : 06/12/2014 11:22 pm
Posts: 2
Free Member
 

[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.


 
Posted : 07/12/2014 12:09 am

6 DAYS LEFT
We are currently at 95% of our target!