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.

有帮助吗?

解决方案 3

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.

其他提示

The pipe is wrong. It should be:

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

You don't need s.txt

You may also try

awk '{ arg=$2 } END {print arg}' scan.txt 

but the output is not visible on my gnome-terminal (due to terminal escape codes at the end of the text, I think..)

You can get visible output on the terminal if you remove the last part (the dots, and the escape code) as e.g.

awk '{ arg=$2 } END {sub(/\.\..*$/,"",arg); print arg}' scan.txt

produces

/home/gustaf/.cache/mozilla/firefox/mwad0hks.default/startupCache/
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top