Replace dot & comma with blank in logstash filter - logstash-configuration

I want to replace every dot and comma with a blank in logstash but its not working.
The code below is not giving any errors, but it does not replace the dots and commas:
gsub => ["new_padd", "[.,]", "",
"new_ladd", "[.,]", ""]

You can use regular expression code is like below:
let str="This is simple, text.";
var x= str.replace(/[.,]/g, '');
output: This is simple text

Related

How to search and replace a string while preserving variable part of the string in vim

I want to search and replace a string in vim with preserving the variable part of string. for example:
StringA("variable1")
StringA("variable2")
StringA("variable3")
StringA("variable4")
replace this with:
StringA("variable1", new_constant_string)
StringA("variable2", new_constant_string)
StringA("variable3", new_constant_string)
StringA("variable4", new_constant_string)
What i want to do is search for
s/StringA(*)
and replace it with
s/StringA(*)/StringA(*, new_constant_string)
where (*= variable1,variable2,variable3,variable4 and is preserved)
You can also filter the lines using g and then apply s
:g/StringA(/s/)$/, new_constant_string)/
:g/StringA(/ all lines containing StringA(
s/)$/, new_constant_string)/ substitute ) at end of line with , new_constant_string)
remove $ is there can be characters after ) in the line
Another method:
:%s/StringA(".\{-}"/&, new_constant_string

Ruby Regex Group Replacement

I am trying to perform regular expression matching and replacement on the same line in Ruby. I have some libraries that manipulate strings in Ruby and add special formatting characters to it. The formatting can be applied in any order. However, if I would like to change the string formatting, I want to keep some of the original formatting. I'm using regex for that. I have the regular expression matching correctly what I need:
mystring.gsub(/[(\e\[([1-9]|[1,2,4,5,6,7,8]{2}m))|(\e\[[3,9][0-8]m)]*Text/, 'New Text')
However, what I really want is the matching from the first grouping found in:
(\e\[([1-9]|[1,2,4,5,6,7,8]{2}m))
to be appended to New Text and replaced as opposed to just New Text. I'm trying to reference the match in the form of
mystring.gsub(/[(\e\[([1-9]|[1,2,4,5,6,7,8]{2}m))|(\e\[[3,9][0-8]m)]*Text/, '\1' + 'New Text')
but my understanding is that \1 only works when using \d or \k. Is there any way to reference that specific capturing group in my replacement string? Additionally, since I am using an asterik for the [], I know that this grouping could occur more than once. Therefore, I would like to have the last matching occurrence yielded.
My expected input/output with a sample is:
Input: "\e[1mHello there\e[34m\e[40mText\e[0m\e[0m\e[22m"
Output: "\e[1mHello there\e[40mNew Text\e[0m\e[0m\e[22m"
Input: "\e[1mHello there\e[44m\e[34m\e[40mText\e[0m\e[0m\e[22m"
Output: "\e[1mHello there\e[40mNew Text\e[0m\e[0m\e[22m"
So the last grouping is found and appended.
You can use the following regex with back-reference \\1 in the replacement:
reg = /(\\e\[(?:[0-9]{1,2}|[3,9][0-8])m)+Text/
mystring = "\\e[1mHello there\\e[34m\\e[40mText\\e[0m\\e[0m\\e[22m"
puts mystring.gsub(reg, '\\1New Text')
mystring = "\\e[1mHello there\\e[44m\\e[34m\\e[40mText\\e[0m\\e[0m\\e[22m"
puts mystring.gsub(reg, '\\1New Text')
Output of the IDEONE demo:
\e[1mHello there\e[40mNew Text\e[0m\e[0m\e[22m
\e[1mHello there\e[40mNew Text\e[0m\e[0m\e[22m
Mind that your input has backslash \ that needs escaping in a regular string literal. To match it inside the regex, we use double slash, as we are looking for a literal backslash.

Replace text in brackets gsub

I would like to replace text inside of brackets, and that has a colon and a u.
For example, Here is a link [u:person]! would become Here is a link Person! I am not very experienced with regex, and I am having problems with \1 and $1
Here is the regex that I am using now:
string.gsub(/\[(\w*).*?\]/, "<a href='/user/\1'>\1</a>")
Make the regex /\[\w*:(.*?)\]/ so that person can be captured instead of u. Then use a single quoted string so that \1 isn't interpreted as \x01.
str = "Here is a link [u:person]!"
puts str.gsub(/\[\w*:(.*?)\]/, '\1')
# => Here is a link person!
I changed your regular expression to this, so that person is captured:
/\[\w*:(.*?)\]/
And then replaced it with this String:
"#{$1.capitalize}"
You were close with $1, it just needs to be evaluated as Ruby (using String interpolation, inside a block):
string.gsub(/\[\w*:(.*?)\]/) { "#{$1.capitalize}" }
You could use a regex like this:
/\[u\:([\S]+)\]/
and replace it with:
<a href='/user/#{$1}'>#{$1}</a>
Here's a breakdown of what it the regex does:
First, we have \[, which is just the literal [ character
Next, we have u and \:, which are the literal u and : character respectively
Next, we have ([\S]). The parentheses make a capturing group, which is what #{$1} will be filled in with in the replace part of the regex. [\S]+ looks for all non-whitespace characters.
Lastly, we have \], which is just the literal ] character.
Your code should look something like this:
string.gsub('/\[u\:([\S]+)\]/', '<a href='/user/#{$1}'>#{$1}</a>')
Here is a live test of the regex: https://regex101.com/r/vK0iO2
\[[^\]]*u:([^\]]*)\]
Try this.Replace by <a href='/user/\1'>\1</a>.See demo.
https://regex101.com/r/gX5qF3/13

Reformatting dates

I'm trying to reformat German dates (e.g. 13.03.2011 to 2011-03-13).
This is my code:
str = "13.03.2011\n14:30\n\nHannover Scorpions\n\nDEG Metro Stars\n60\n2 - 3\n\n\n\n13.03.2011\n14:30\n\nThomas Sabo Ice Tigers\n\nKrefeld Pinguine\n60\n2 - 3\n\n\n\n"
str = str.gsub("/(\d{2}).(\d{2}).(\d{4})/", "/$3-$2-$1/")
I get the same output like input. I also tried my code with and without leading and ending slashes, but I don't see a difference. Any hints?
I tried to store my regex'es in variables like find = /(\d{2}).(\d{2}).(\d{4})/ and replace = /$3-$2-$1/, so my code looked like this:
str = "13.03.2011\n14:30\n\nHannover Scorpions\n\nDEG Metro Stars\n60\n2 - 3\n\n\n\n13.03.2011\n14:30\n\nThomas Sabo Ice Tigers\n\nKrefeld Pinguine\n60\n2 - 3\n\n\n\n"
find = /(\d{2}).(\d{2}).(\d{4})/
replace = /$3-$2-$1/
str = str.gsub(find, replace)
TypeError: no implicit conversion of Regexp into String
from (irb):4:in `gsub'
Any suggestions for this problem?
First mistake is the regex delimiter. You do not need place the regex as string. Just place it inside a delimiter like //
Second mistake, you are using captured groups as $1. Replace those as \\1
str = str.gsub(/(\d{2})\.(\d{2})\.(\d{4})/, "\\3-\\2-\\1")
Also, notice I have escaped the . character with \., because in regex . means any character except \n

Why doesn't this Ruby replace regex work as expected?

Consider the following string which is a C fragment in a file:
strcat(errbuf,errbuftemp);
I want to replace errbuf (but not errbuftemp) with the prefix G-> plus errbuf. To do that successfully, I check the character after and the character before errbuf to see if it's in a list of approved characters and then I perform the replace.
I created the following Ruby file:
line = " strcat(errbuf,errbuftemp);"
item = "errbuf"
puts line.gsub(/([ \t\n\r(),\[\]]{1})#{item}([ \t\n\r(),\[\]]{1})/, "#{$1}G\->#{item}#{$2}")
Expected result:
strcat(G->errbuf,errbuftemp);
Actual result
strcatG->errbuferrbuftemp);
Basically, the matched characters before and after errbuf are not reinserted back with the replace expression.
Anyone can point out what I'm doing wrong?
Because you must use syntax gsub(/.../){"...#{$1}...#{$2}..."} or gsub(/.../,'...\1...\2...').
Here was the same problem: werid, same expression yield different value when excuting two times in irb
The problem is that the variable $1 is interpolated into the argument string before gsub is run, meaning that the previous value of $1 is what the symbol gets replaced with. You can replace the second argument with '\1 ?' to get the intended effect. (Chuck)
I think part of the problem is the use of gsub() instead of sub().
Here's two alternates:
str = 'strcat(errbuf,errbuftemp);'
str.sub(/\w+,/) { |s| 'G->' + s } # => "strcat(G->errbuf,errbuftemp);"
str.sub(/\((\w+)\b/, '(G->\1') # => "strcat(G->errbuf,errbuftemp);"

Resources