AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 406630
Accepted
Philip Kirkbride
Philip Kirkbride
Asked: 2017-11-24 10:06:11 +0800 CST2017-11-24 10:06:11 +0800 CST 2017-11-24 10:06:11 +0800 CST

grep --line-buffered 直到 X 行?

  • 772

我正在查看日志并想检测程序是否有 3 次尝试失败:

tail -f file.log | grep --line-buffered program\ failed\ string

如果从grep命中 3 的行数我想返回一个错误。

我怎样才能做到这一点?

grep
  • 2 2 个回答
  • 977 Views

2 个回答

  • Voted
  1. Best Answer
    user285259
    2017-11-24T10:32:52+08:002017-11-24T10:32:52+08:00

    awk是一个很棒的流扫描工具。

    我认为它应该显示所有行以查看日志,直到它退出,这与您使用grep的示例相反,该示例仅显示错误行。

    tail -f file.log | awk '
        BEGIN {
            count = 0
        }
    
        {
            print($0)
    
            if ($0 ~ /program failed/) {
                count++
                if (count == 3) {
                    exit(1)
                }
            }
        }
    '
    

    您可以将 awk 代码移动到tail.awk并根据需要调用tail -f file.log | awk -f tail.awk。

    等效地,以更紧凑的形式:

    tail -f file.log | awk '1; /program failed/ && ++count == 3 { exit 1 }'
    
    • 6
  2. igal
    2017-11-24T11:25:58+08:002017-11-24T11:25:58+08:00

    以防万一有人可能更喜欢 Python 替代品:

    #!/usr/bin/env python2
    # -*- encoding: ascii -*-
    """tail.py"""
    
    import sys
    import argparse
    import time
    import re
    
    # Create a command-line parser
    parser = argparse.ArgumentParser()
    
    parser.add_argument(
        "-f", "--input-file",
        help="File to search through.",
        dest="input_file", type=str,
    )
    parser.add_argument(
        "-p", "--pattern",
        help="Regular expression to match.",
        default=r'.*',
        dest="pattern", type=str,
    )
    parser.add_argument(
        "-m", "--match-limit",
        help="Number of matches before exiting.",
        default=float(1),
        dest="match_limit", type=int,
    )
    parser.add_argument(
        "-q", "--quiet",
        help="Don't print matched lines.",
        default=False,
        dest="quiet", type=bool,
    )
    
    # Define the file-watching function
    def watch_for_matches(file_handle, pattern, match_limit, quiet):
    
        # Count the number of matched lines
        matches_found = 0
    
        # Get the next line
        line = file_handle.readline()
    
        # Check to see if the limit has been reached
        while(matches_found < match_limit):
    
            # Match the line against the given regular expression
            if(line and re.search(pattern, line)):
    
                # Optionally print the matched line
                if not quiet:
                    sys.stdout.write(line)
    
                # Increment the match counter
                matches_found += 1
    
            # Optionally wait for a moment 
            time.sleep(0.25)
    
            # Get the next line of input
            line = file_handle.readline()
    
        # If the match limit is reached, exit with an error
        sys.exit(1)
    
    # Parse the command-line arguments
    args = parser.parse_args()
    
    # Execute the function
    if args.input_file:
        with open(args.input_file, 'r') as file_handle:
            watch_for_matches(file_handle, args.pattern, args.match_limit, args.quiet)
    
    # If no file is given, use standard input instead
    else:
        watch_for_matches(sys.stdin, args.pattern, args.match_limit, args.quiet)
    
    • 2

相关问题

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    JSON数组使用jq来bash变量

    • 4 个回答
  • Marko Smith

    日期可以为 GMT 时区格式化当前时间吗?[复制]

    • 2 个回答
  • Marko Smith

    bash + 通过 bash 脚本从文件中读取变量和值

    • 4 个回答
  • Marko Smith

    如何复制目录并在同一命令中重命名它?

    • 4 个回答
  • Marko Smith

    ssh 连接。X11 连接因身份验证错误而被拒绝

    • 3 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Marko Smith

    systemctl 命令在 RHEL 6 中不起作用

    • 3 个回答
  • Marko Smith

    rsync 端口 22 和 873 使用

    • 2 个回答
  • Marko Smith

    以 100% 的利用率捕捉 /dev/loop -- 没有可用空间

    • 1 个回答
  • Marko Smith

    jq 打印子对象中所有的键和值

    • 2 个回答
  • Martin Hope
    EHerman JSON数组使用jq来bash变量 2017-12-31 14:50:58 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST
  • Martin Hope
    Drux 日期可以为 GMT 时区格式化当前时间吗?[复制] 2017-12-26 11:35:07 +0800 CST
  • Martin Hope
    AllisonC 如何复制目录并在同一命令中重命名它? 2017-12-22 05:28:06 +0800 CST
  • Martin Hope
    Steve “root”用户的文件权限如何工作? 2017-12-22 02:46:01 +0800 CST
  • Martin Hope
    Bagas Sanjaya 为什么 Linux 使用 LF 作为换行符? 2017-12-20 05:48:21 +0800 CST
  • Martin Hope
    Cbhihe 将默认编辑器更改为 vim for _ sudo systemctl edit [unit-file] _ 2017-12-03 10:11:38 +0800 CST
  • Martin Hope
    showkey 如何下载软件包而不是使用 apt-get 命令安装它? 2017-12-03 02:15:02 +0800 CST
  • Martin Hope
    youxiao 为什么目录 /home、/usr、/var 等都具有相同的 inode 编号 (2)? 2017-12-02 05:33:41 +0800 CST
  • Martin Hope
    user223600 gpg —list-keys 命令在将私钥导入全新安装后输出 uid [未知] 2017-11-26 18:26:02 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve