-2

I'm trying to draw disk data from a text file to be used in an analysis program. The data from the file comes from the output of a Linux command free -m and looks like this:

           total       used       free     shared    buffers     cached
Mem:        7976       6459       1517          0        865       2248
-/+ buffers/cache:     3344       4631
Swap:       1951          0       1951

I want to extract the 6 numbers on the second row using regex in Python and put them either in another file, send them directly to a program's input, or simply just put their values into variables, but I'm a bit rusty with regex logic and syntax. Any pointers?

Barmar
  • 741,623
  • 53
  • 500
  • 612
rx150
  • 1
  • 1
  • 1
    Possible duplicate of [Find numbers in a sentence by regex](http://stackoverflow.com/questions/12419998/find-numbers-in-a-sentence-by-regex) – PyNEwbie May 06 '16 at 00:17
  • Instead of parsing the output of `free -m`, it would probably be easier to get what you want from `/proc/meminfo`. – Barmar May 06 '16 at 01:21

1 Answers1

0

You're probably better off without using regex. Assuming that there's only one entry in a file and there's no empty lines between the rows you could use following:

with open('test.txt') as f:
    f.readline()
    nums = [int(x) for x in f.readline().split()[1:7]]
niemmi
  • 17,113
  • 7
  • 35
  • 42