显示发现参数的文件的行

发布于 2025-01-21 06:30:59 字数 684 浏览 2 评论 0原文

我想制作一个简单的脚本,其中用户输入参数以及参数存在的所有行都出现在屏幕上。 该脚本将用于使我的日志文件之一中的搜索(非常长并且不断更新)更容易。 我曾想过要为此使用GREP,然后我终于想到了更改为Glob的想法,这更好,因为我们可以使用几个参数。

但是代码似乎不起作用。当我尝试任何事情时,什么都不会显示。我是Python的初学者,我看不出我的代码到底出了什么问题。

如果代码有效,则应显示存在给定参数的日志文件中的所有行。但事实并非如此。

import re
import sys
import glob
import os

if len(sys.argv) != 2:
    print("Syntaxe: %s <UserID or IP Address>" % sys.argv[0].split('/')[-1])
    sys.exit(1)

file = os.path.join("/applis/tacacs/log/tacacs.log")

for arg in sys.argv[2:]:
    for file in glob.iglob(arg):
        for line in open(file, 'r'):
            if re.search(sys.argv[1], line):
                print line,

有人可以告诉我我的代码怎么了?提前致谢。

I want to make a simple script where the user enters an argument and all the lines where the argument exists appear on the screen.
This script will be used to make the search in one of my log files (which is very long and constantly updated) easier.
I had thought of using grep for this, then I finally got the idea to change to glob which is better since we can work with several arguments.

But the code doesn't seem to work. When I try anything, nothing is displayed. I am a beginner in python and I can't see what exactly is wrong with my code.

In case the code works, it should show all lines in the log file where the given argument is present. But this is not the case.

import re
import sys
import glob
import os

if len(sys.argv) != 2:
    print("Syntaxe: %s <UserID or IP Address>" % sys.argv[0].split('/')[-1])
    sys.exit(1)

file = os.path.join("/applis/tacacs/log/tacacs.log")

for arg in sys.argv[2:]:
    for file in glob.iglob(arg):
        for line in open(file, 'r'):
            if re.search(sys.argv[1], line):
                print line,

Could someone please tell me what is wrong with my code? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文