Grep等效于Python [英] Grep Equivalent for Python

查看:106
本文介绍了Grep等效于Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我来自shell / perl背景,只是学习python。要从
开始,我正在尝试使用/ proc FS从Linux

服务器获取系统信息。例如,为了获得服务器上物理内存的金额,我会在shell中执行以下操作:


grep ^ MemTotal / proc / meminfo | awk''{print $ 2}''


这会得到我需要的确切数字。现在,我正试图在python中做这个
。这是我到目前为止的地方:


memFile = open(''/ proc / meminfo'')

for memFile.readlines()中的行:

print re.search(''MemTotal'',line)

memFile.close()


我想是什么我正在尝试逻辑上做的是......逐行阅读文件

,抓住我想要的模式并将其分配给变量。

以上它并没有真正起作用,它返回的内容类似于

"< _sre.SRE_Match object at 0xb7f9d6b0>"找到匹配。


任何帮助都会非常感激。

Tom

解决方案

2}''


这会得到我需要的确切数字。现在,我正试图在python中做这个
。这是我到目前为止的地方:


memFile = open(''/ proc / meminfo'')

for memFile.readlines()中的行:

print re.search(''MemTotal'',line)

memFile.close()


我想是什么我正在尝试逻辑上做的是......逐行阅读文件

,抓住我想要的模式并将其分配给变量。

以上它并没有真正起作用,它返回的内容类似于

"< _sre.SRE_Match object at 0xb7f9d6b0>"找到匹配的时候。


任何帮助都会非常感激。

Tom


tereglow写道:


大家好,


我来自shell / perl背景并且刚刚学习python。要从
开始,我正在尝试使用/ proc FS从Linux

服务器获取系统信息。例如,为了获得服务器上物理内存的金额,我会在shell中执行以下操作:


grep ^ MemTotal / proc / meminfo | awk''{print


2}''


这会得到我需要的确切数字。现在,我正试图在python中做这个
。这是我到目前为止的地方:


memFile = open(''/ proc / meminfo'')

for memFile.readlines()中的行:

print re.search(''MemTotal'',line)

memFile.close()


我想是什么我正在尝试逻辑上做的是......逐行阅读文件

,抓住我想要的模式并将其分配给变量。

以上它并没有真正起作用,它返回的内容类似于

"< _sre.SRE_Match object at 0xb7f9d6b0>"找到匹配项。


如果您有任何帮助,将不胜感激。

Tom



这里不需要正则表达式。未经测试的代码如下:


for open(''/ proc / meminfo'')。readlines:

if line.startswith(" Memtotal: ):

name,amt,unit = line.split()

打印名称,amt,unit

break


问候

Steve

-

Steve Holden +44 150 684 7255 +1 800 494 3119

Holden Web LLC / Ltd http://www.holdenweb.com

Skype:holdenweb http://del.icio.us /steve.holden

博客注: http:// holdenweb.blogspot.com

在PyCon见到你? http://us.pycon.org/TX2007


Hello all,

I come from a shell/perl background and have just to learn python. To
start with, I''m trying to obtain system information from a Linux
server using the /proc FS. For example, in order to obtain the amount
of physical memory on the server, I would do the following in shell:

grep ^MemTotal /proc/meminfo | awk ''{print $2}''

That would get me the exact number that I need. Now, I''m trying to do
this in python. Here is where I have gotten so far:

memFile = open(''/proc/meminfo'')
for line in memFile.readlines():
print re.search(''MemTotal'', line)
memFile.close()

I guess what I''m trying to logically do is... read through the file
line by line, grab the pattern I want and assign that to a variable.
The above doesn''t really work, it comes back with something like
"<_sre.SRE_Match object at 0xb7f9d6b0>" when a match is found.

Any help with this would be greatly appreciated.
Tom

解决方案

2}''

That would get me the exact number that I need. Now, I''m trying to do
this in python. Here is where I have gotten so far:

memFile = open(''/proc/meminfo'')
for line in memFile.readlines():
print re.search(''MemTotal'', line)
memFile.close()

I guess what I''m trying to logically do is... read through the file
line by line, grab the pattern I want and assign that to a variable.
The above doesn''t really work, it comes back with something like
"<_sre.SRE_Match object at 0xb7f9d6b0>" when a match is found.

Any help with this would be greatly appreciated.
Tom


tereglow wrote:

Hello all,

I come from a shell/perl background and have just to learn python. To
start with, I''m trying to obtain system information from a Linux
server using the /proc FS. For example, in order to obtain the amount
of physical memory on the server, I would do the following in shell:

grep ^MemTotal /proc/meminfo | awk ''{print


2}''

That would get me the exact number that I need. Now, I''m trying to do
this in python. Here is where I have gotten so far:

memFile = open(''/proc/meminfo'')
for line in memFile.readlines():
print re.search(''MemTotal'', line)
memFile.close()

I guess what I''m trying to logically do is... read through the file
line by line, grab the pattern I want and assign that to a variable.
The above doesn''t really work, it comes back with something like
"<_sre.SRE_Match object at 0xb7f9d6b0>" when a match is found.

Any help with this would be greatly appreciated.
Tom

Regular expressions aren''t really needed here. Untested code follows:

for line in open(''/proc/meminfo'').readlines:
if line.startswith("Memtotal:"):
name, amt, unit = line.split()
print name, amt, unit
break

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Blog of Note: http://holdenweb.blogspot.com
See you at PyCon? http://us.pycon.org/TX2007


这篇关于Grep等效于Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆