Ruby Filter to delete array element with regexp

Hi, I try to delete an array element using a regexp, But code isn't run.

Imput json:

`{"test":"The http module allows to expose\nThe http module allows to expose\ncamp:using asynchronous communication\nusing asynchronous communication"}`

In this case, I want to delete any element that has "http module" in value.

Pipiline:

input{
    stdin{
        codec => json {
         } 
    }
}

filter{

    ruby{
        code => '
           matches = event.get("test").split("\n")
            matches.each_index{ |x|
            if matches[x] =~ ".*http module.*"
            else 
               matches.delete_at(x)
            end
           }
           matches.each_index{ |x|
           event.set("line#{x+1}", matches[x])
           }
        '   
    }  
        
    
}
output {
    stdout {
        codec => rubydebug
    } 
}

I want the result:

line1:using asynchronous communication
line1:using asynchronous communication

You cannot modify an array using delete_at whilst iterating over it

    code => '
        matches = event.get("test").split("\n")
        matches = matches.delete_if { |x| x =~ /http module/ }
        matches.each_index{ |x|
            event.set("line#{x+1}", matches[x])
        }
    '
2 Likes

Thanks for your help. Working perfectly!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.