Viewing 14 posts - 1 through 14 (of 14 total)
  • Linux/python dudes – how do I extract this data?
  • sharkbait
    Free Member

    I have a long log file containing temperatures that is updated every 10 mins – lines are formatted date/time/temp1/temp2.
    For example: 06/12/2014 20:50:01 56.500 15.000

    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 tail -c13 to get both temps and then head -c6 to get the first temp but it seems a bit clumsy. Would len be any help?

    Any thoughts?

    jam-bo
    Full Member

    Spilt()

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

    GrahamS
    Full Member

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

    sharkbait
    Free Member

    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.

    sas
    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

    samuri
    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

    sharkbait
    Free Member

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

    andytherocketeer
    Full Member

    backticks don’t work on here.

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

    oldnpastit
    Full Member

    @Samurai

    Perhaps this?


    temp1=$(tail -1 tempfile |awk '{print $2}')

    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.

    sharkbait
    Free Member

    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

    GrahamS
    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)

    sharkbait
    Free Member

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

    samuri
    Free Member

    temp1=$(tail -1 tempfile |awk ‘{print $2}’)

    The “$” at the start looks like you’ve been hacking on perl too long….

    php. I’ve been building phishing pages for the last few weeks.

Viewing 14 posts - 1 through 14 (of 14 total)

The topic ‘Linux/python dudes – how do I extract this data?’ is closed to new replies.