无聊的AWK高手能否转换一下这个Python程序?

发布于 2024-09-28 19:45:45 字数 756 浏览 3 评论 0原文

我喜欢 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

提赋 2024-10-05 19:45:45

为什么叫 awk?

对我来说,这看起来像是一个简单的 grep 命令;类似于:

egrep -w '628251|173511|223401|138276|673278|698450|629138|449040|901575' /Users/Zero/Documents/MyProgram.app/Contents/TempFiles/*.log*

update: 或使用 find+grep,如一些评论中的建议,如果打算进行递归搜索

Why awk?

This looks like a simple grep command to me; something like:

egrep -w '628251|173511|223401|138276|673278|698450|629138|449040|901575' /Users/Zero/Documents/MyProgram.app/Contents/TempFiles/*.log*

update: or use find+grep, as suggested in some of the comments, if a recursive search is intended

最终幸福 2024-10-05 19:45:45
BEGIN{
   id="628251 173511 223401 138276 673278 698450 629138 449040 901575"
   m=split(id,ID," ")
   for(i=1;i<ARGC;i++){
       while( (getline line<ARGV[i] ) > 0 ){
           n=split(line,LINE," ")
           for ( o=1; o<=n; o++){
                for(num in ID){
                   if ( num == LINE[o] ){
                     print line
                   }
                }
           }
       }
   }
}

保存为 myscript.awk ,然后

#!/bin/bash
ROOT = "/Users/Zero/Documents/MyProgram.app/Contents/TempFiles"
cd $ROOT
awk -f myscript.awk file* #do for files that start with "file"

@OP,

对于文本/文件处理,awk 不会输给 Perl 或 Python 或任何其他。如果您(或其他认为 awk 已过时的人)感兴趣,请访问 http://awk.info。不,awk 在现代环境中仍然有其用途。不要让任何人告诉你其他情况

BEGIN{
   id="628251 173511 223401 138276 673278 698450 629138 449040 901575"
   m=split(id,ID," ")
   for(i=1;i<ARGC;i++){
       while( (getline line<ARGV[i] ) > 0 ){
           n=split(line,LINE," ")
           for ( o=1; o<=n; o++){
                for(num in ID){
                   if ( num == LINE[o] ){
                     print line
                   }
                }
           }
       }
   }
}

save as myscript.awk , then

#!/bin/bash
ROOT = "/Users/Zero/Documents/MyProgram.app/Contents/TempFiles"
cd $ROOT
awk -f myscript.awk file* #do for files that start with "file"

@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

深海不蓝 2024-10-05 19:45:45

在 TAWK 和 GAWK 中,这可以工作并利用 awk 的自动循环和简洁的能力

    BEGIN{
       id="628251 173511 223401 138276 673278 698450 629138 449040"
       gsub(" ", "|", id)
    }
    $0 ~ id {print($0); close(FILENAME)}

In TAWK and GAWK this works and exploits awk's automatic loop and talent for being concise

    BEGIN{
       id="628251 173511 223401 138276 673278 698450 629138 449040"
       gsub(" ", "|", id)
    }
    $0 ~ id {print($0); close(FILENAME)}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文