Search code examples
linuxawktail

How to get data from file with tail and awk


I have a file that I want to get some data from. I running GNU/Linux.

File: http://pastebin.com/AXF4SJAm

I running this command for get the data:

tail -n 1 scan.txt > s.txt | awk '/%/ { print $2 }' s.txt

Wanted output:

/home/gustaf/.cache/mozilla/firefox/mwad0hks.default/startupCache/...[2K

But I get this instead: [~100.0%] /home/gustaf/.cache/mozilla/firefox/mwad0hks.default/startupCache/...[2K

How should I do to get the desiand result?

More details:

For generate the file, I using this command: avgscan --heur /home/gustaf > scan.txt

and during the run I've tried both solutions with the same result:

/home/gustaf/.mozilla/firefox/mwad0hks.default/ghostery/patterns-...[2K
[~5.1%]

And I using scan.txt in a python script.


Solution

  • I have solved the problem.

    Before:

    cmd = "awk '{ arg=$2 } END {sub(/\.\..*$/,arg); print arg}' scan.txt"
    x = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    AvgAPI.lastscanned = x.stdout.read()
    

    Now:

    Line_len = 1200
    SEEK_END = 2
    file = open('scan.txt', "r")
    file.seek(-Line_len, SEEK_END)
    data_scanfile_not_cleaned = str(file.read(Line_len)).split(" ")[1].strip()
    if not data_scanfile_not_cleaned.startswith('/'):
       file.close()
       AvgAPI.lastscanned = ""
       time.sleep(0.1)
    else:
       data_scanfile_re = re.sub(r'[~\s+(\d+)%]','',data_scanfile_not_cleaned)
       data_scanfile_strip = data_scanfile_re.strip("[.]")
       data_scanfile = data_scanfile_strip.strip("[K")
       AvgAPI.lastscanned = data_scanfile
       file.close()
       time.sleep(0.1)
    

    There are some minor flaws with the new solution, but it works satisfactorily.