I have a dir containing logstash config files these files have filter blocks, and sometimes nested child blocks
need to match whole filter block
filter { any text till final matching closing brace for filter block is found
or
filter
{ any text till final matching closing brace for filter block is found
till its final ending braces, because there are sometimes many nested blocks that have opening ending braces pair as well.
I'm using grep & xargs + sed to make changes in matched block.
need a regex for both grep & sed so changes are only made within the block.
SAMPLE 1
filter {
json{
skip_on_invalid_json => true
source => "message"
}
mutate {
convert => { "rootComponent" => "boolean" }
}
date {
match => ["timestamp","yyyy-MM-dd HH:mm:ss"]
target => "timestamp"
}
ruby {
code => "
require 'date'
week_n = event.get('timestamp').time.strftime '%V'
month_n = event.get('timestamp').time.strftime '%m'
year_n = event.get('timestamp').time.strftime '%Y'
if(week_n == '01' && month_n == '12')
#year_n = (year_n.to_i + 1) # LINE TO BE UNCOMMENTED
week_num = year_n.to_s + 'w' + week_n.to_s
else if (month_n == '01' && week_n.to_i > 50)
year_n = (year_n.to_i - 1)
week_num = year_n.to_s + 'w' + week_n.to_s
else
week_num = year_n.to_s + 'w' + week_n.to_s
end
end
event.set('[@metadata][week_num]', week_num)
"
}
if([expiresOn] == "N/A") {
prune {
blacklist_names => ["log","tags","agent","message","path","@version","host","ecs","input","cloud","expiresOn"]
}
} else {
prune {
blacklist_names => ["log","tags","agent","message","path","@version","host","ecs","input","cloud"]
}
}
}
Line To Edit:
#year_n = (year_n.to_i + 1) # LINE TO BE UNCOMMENTED
SAMPLE 2
filter{
json {
skip_on_invalid_json => true
source => "message"
}
mutate {
convert => { "rootComponent" => "boolean" }
}
mutate {
add_field => {
"[@metadata][eventName]" => "%{eventName}"
"kyc.region" => "N/A"
}
}
ruby {
code => "
require 'date'
week_n = event.get('addKycTimestamp').time.strftime '%V'
month_n = event.get('addKycTimestamp').time.strftime '%m'
year_n = event.get('addKycTimestamp').time.strftime '%Y'
if(week_n == '01' && month_n == '12')
#year_n = (year_n.to_i + 1) # LINE TO BE UNCOMMENTED
This edit should only occur in filter blocks. Do not edit the line if it appears in other blocks.
I tried below
grep -rl '(?s)filter\s*\{(?:[^{}]+|(?0))*?\year_n =\s*=>(?:[^{}]+|(?0))*?\}' config_dir \
| xargs -r -I{} sed -i -E '/^[[:space:]]*filter[[:space:]]*\{/,/^[[:space:]]*output[[:space:]]*\{/ s;^[[:space:]]*#([[:space:]]*year_n =\s*=>.*);\1;'
{ outer { inner }... does the ending brace belong toouterorinner?grep,xargs,sed) code you've tried and the (wrong) results generated by said code; also verify if you want the original file updated (if you're piping the results of thegreptosedthen that's a problem sincesedis not working with the original file ... hence the need for more details as well as your current code)-zoption but that's not the best approach ... Some tools are better suited for this ... See for example Print content between first matching brackets