Particular line match using regex

前端 未结 4 933
挽巷
挽巷 2021-01-28 01:09
This script may take a while to run, especially on a busy podmaster.
Generating Syslog TopN list on node for last 3 hours.

        Top 5 hosts for Day: Oct8 between 02:         


        
相关标签:
4条回答
  • 2021-01-28 01:23

    There's nothing wrong with your regex.

    That's just a formatting difference:

    use strict;
    use warnings;
    
    my $data = do {local $/; <DATA>};
    
    my @ones = $data =~ m/^\s*[1]\s+([\d]+)/mg;
    
    for my $i (0..$#ones) {
        printf "\$%d = %s\n", $i+1, $ones[$i];
    }
    
    __DATA__
            Rank       Number of Alerts Host
            ----       ---------------- ----
            1       3124        abc
            2       2294        bcd
    
    
    
            Rank       Number of Alerts Host
            ----       ---------------- ----
            1       5495        cdf
            2       2625        klm
    
    
            Rank       Number of Alerts Host
            ----       ---------------- ----
            1       2747        lll
            2       876         jjj
    

    Outputs:

    $1 = 3124
    $2 = 5495
    $3 = 2747
    

    live demo

    0 讨论(0)
  • 2021-01-28 01:33

    Change your regex to the below if the input contains the exact string you posted.

    (?s)\s+1+\s+([\d]+).*?\s+1+\s+([\d]+).*?\s+1+\s+([\d]+)
    

    DEMO

    For javascript,

    \s+1+\s+([\d]+)[\S\s]*?\s+1+\s+([\d]+)[\S\s]*?\s+1+\s+([\d]+)
    

    DEMO

    0 讨论(0)
  • 2021-01-28 01:33
    Rank.*?\n.*?\n1\s*(\d+)
    

    Try this.Set flag g.

    See demo.

    http://regex101.com/r/hQ1rP0/58

    0 讨论(0)
  • 2021-01-28 01:34

    Noticed this in your duplicate question about IPautomata designer

    We are using new IPsoft tool which has two options.

    1. Line Filter(JavaScript regex)
    2. Match(JavaScript regex)
    

    For example if we need to match the alertcount(3124). First filter the line >than match the same. Like that I need to match another two alert count(5495 >and 2747). Please help me to sort it out.

    If you are trying to capture the output from a host command state, or any state's OUTPUT variable, you can use the Extract Variable tab: Name: capture_variable Line Filter: \s+1\s+.* Match: .*1\s+(\d+).* Replace: $1

    this will set the capture_variable equal to a multi-line string like this:

    3124
    5495
    2747
    
    0 讨论(0)
提交回复
热议问题