无聊的AWK高手能否转换一下这个Python程序?
我喜欢 Python,但不太关心 AWK。为了进行比较(并了解 Python 到 AWK 的大师如何做到这一点),有人可以在 AWK 中重写以下 Python 程序吗?考虑到它的长度,有些人会认为重写对于任何有一点时间的人来说都是简单易行的。
import os
ROOT = '/Users/Zero/Documents/MyProgram.app/Contents/TempFiles'
ID = '628251 173511 223401 138276 673278 698450 629138 449040 901575'.split()
def main():
for name in os.listdir(ROOT):
if '.log' in name.lower():
path = os.path.join(ROOT, name)
if os.path.isfile(path):
data = open(path, 'rb').read()
for line in data.split('\r'):
for number in ID:
if number in line:
print line
break
if __name__ == '__main__':
main()
I love Python but do not really care for AWK. For purposes of comparison (and to see how a Python-to-AWK master would do this), could someone rewrite the following Python program in AWK? Considering how short it is, some would think that the rewrite would be simple and easy for anyone with a little time.
import os
ROOT = '/Users/Zero/Documents/MyProgram.app/Contents/TempFiles'
ID = '628251 173511 223401 138276 673278 698450 629138 449040 901575'.split()
def main():
for name in os.listdir(ROOT):
if '.log' in name.lower():
path = os.path.join(ROOT, name)
if os.path.isfile(path):
data = open(path, 'rb').read()
for line in data.split('\r'):
for number in ID:
if number in line:
print line
break
if __name__ == '__main__':
main()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么叫 awk?
对我来说,这看起来像是一个简单的 grep 命令;类似于:
update: 或使用 find+grep,如一些评论中的建议,如果打算进行递归搜索
Why awk?
This looks like a simple grep command to me; something like:
update: or use find+grep, as suggested in some of the comments, if a recursive search is intended
保存为 myscript.awk ,然后
@OP,
对于文本/文件处理,awk 不会输给 Perl 或 Python 或任何其他。如果您(或其他认为 awk 已过时的人)感兴趣,请访问 http://awk.info。不,awk 在现代环境中仍然有其用途。不要让任何人告诉你其他情况
save as
myscript.awk
, then@OP,
For text/file processing, awk doesn't lose to Perl or Python or any others. If you (or others here thinking awk is obsoleted) are interested, go to http://awk.info. And no, awk still has its uses in the modern environment. don't let anyone tell you otherwise
在 TAWK 和 GAWK 中,这可以工作并利用 awk 的自动循环和简洁的能力
In TAWK and GAWK this works and exploits awk's automatic loop and talent for being concise