<p>这应该通过管道将输出通过<a href="https://docs.python.org/2/library/subprocess.html#subprocess.Popen" rel="nofollow">subprocess.Popen</a>从python运行bash命令,并适用于python2.6:</p>
<pre><code>from subprocess import Popen, PIPE
p1 = Popen(["cat","/proc/meminfo"], stdout=PIPE)
p2 = Popen(["egrep", 'MemTotal|MemFree|Cached|SwapTotal|SwapFree|AnonPages|Dirty|Writeback|PageTables|HugePages_' ], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close()
p3 = Popen(["awk","{ print $2}"],stdin=p2.stdout,stdout=PIPE)
p2.stdout.close()
p4 = Popen(["pr", "-t", "-T", " columns=15", " width=240"],stdin=p3.stdout,stdout=PIPE)
p3.stdout.close()
output = p4.communicate()
print(output[0])
</code></pre>
<p>我的系统的输出是:</p>
<pre><code>16341932 4484840 5105220 0 8388604 8388604 108 0 5106832 78100 0 0 0 0 0
</code></pre>
<p>也可以使用python打开文件,并将文件对象传递给第一个进程:</p>
<pre><code>from subprocess import Popen,PIPE,STDOUT
with open("/proc/meminfo") as f:
p1 = Popen(["egrep", 'MemTotal|MemFree|Cached|SwapTotal|SwapFree|AnonPages|Dirty|Writeback|PageTables|HugePages_' ], stdin=f, stdout=PIPE)
p2 = Popen(["awk","{ print $2}"],stdin=p1.stdout,stdout=PIPE)
p1.stdout.close()
p3 = Popen(["pr", "-t", "-T", " columns=15", " width=240"],stdin=p2.stdout,stdout=PIPE)
p2.stdout.close()
output = p3.communicate()
print(output[0])
</code></pre>
<p>纯python解决方案,使用<code>str.find</code>模拟egrep查找包含文件中pre的任何子字符串的行,并使用<code>str.rsplit</code>获取第二列,即数字:</p>
<pre><code>pre = ('MemTotal', 'MemFree', 'Cached', 'SwapTotal', 'SwapFree', 'AnonPages', 'Dirty', 'Writeback', 'PageTables', 'HugePages_')
with open("/proc/meminfo") as f:
out = []
for line in f:
# if line.find(p) is not -1 we have a match
if any(line.find(p) != -1 for p in pre):
# split twice from the end on whitespace and get the second column
v = line.rsplit(None, 2)[1]
out.append(v)
print(" ".join(out))
</code></pre>
<p>输出:</p>
<pre><code>16341932 4507652 5128624 0 8388604 8388604 48 0 5059044 78068 0 0 0 0 0
</code></pre>
<p>在上面的代码中使用<a href="https://docs.python.org/2/library/functions.html#any" rel="nofollow">any</a>将对匹配项进行延迟求值和短路,如果没有匹配项,它将求值为False,因此不会添加任何内容。你知道吗</p>
<p>我们可以使用<a href="https://docs.python.org/2/library/re.html#search-vs-match" rel="nofollow">re.search</a>编译模式/子字符串来检查:</p>
<pre><code>import re
r = re.compile(r"MemTotal|MemFree|Cached|SwapTotal|SwapFree|AnonPages|Dirty|Writeback|PageTables|HugePages_")
with open("/proc/meminfo") as f:
out =[]
for line in f:
if r.search(line):
v = line.rsplit(None, 2)[1]
out.append(v)
print(" ".join(out))
</code></pre>
<p>输出:</p>
<pre><code>16341932 4507596 5128952 0 8388604 8388604 0 16788 5058092 78464 0 0 0 0 0
</code></pre>
<p>python作为python,我们可以将所有逻辑放在一个列表comp中以获得数据:</p>
<pre><code>pre = ('MemTotal', 'MemFree', 'Cached', 'SwapTotal', 'SwapFree', 'AnonPages', 'Dirty', 'Writeback', 'PageTables', 'HugePages_')
with open("/proc/meminfo") as f:
out = [line.rsplit(None, 2)[1] for line in f if r.search(line)]
print(" ".join(out))
</code></pre>
<p>输出:</p>
<pre><code>16341932 4443796 5133420 0 8388604 8388604 120 0 5118004 78572 0 0 0 0 0
</code></pre>