Gsub replacing \\n with \n

(Saram Ali) #1

I want to replace '\\n' with '\n'

i am using the gsub method but cannot replace


code => "@mystring=event.get('stockLines');
@mystring=@mystring.gsub('\\n', '\n');"


(Saram Ali) #2



I'm not sure but I think you should use gsub inside a mutate filter instead of trying to write some ruby code. See
Anyway, I think the problem is with the double backslash: the first backslash is actually used to escape the second backslash so you lack one backslash. And in the result you expect "\n", you also have to escape that backslash, for it to become literal.
I would try something like:

filter {
  mutate {
    gsub => [
      "stockLines", "\\\\n", "\\n",

If it doesn't work, please tell us what you've tried and what the result was.

Edit: I also found this, if you really prefer using Ruby:

(Saram Ali) #4

Thanks for writing.
I tried using mutate as you suggested. But still the issue is there, I can't seem to remove the \\n from my log.

code => "@stockLines = @stockLines.to_s + event.get('stockInformation') + '\n';
if event.get('stockInformation') =~ /Time taken for Variance Price Calculations/
event.set('stockLog', @stockLines);
event.set('prev_stock_id', @@map['document_id' + @@map['currentStockNumber']]);

gsub => [
  "stockLog", "\\\\n", "\\n"


You can take a look at this topic, I had a similar issue with backslashes and the workaround is a bit ugly Parse nested key-values


Your 'stockInformation' field already ends with a backslash, right? I guess that's why you have \\n in the end. Maybe you can try to remove that backslash first with:

mutate {
   gsub => [
     "stockInformation", "[\\]", " "

And then add the "\n" to your stockLog.

(Saram Ali) #7

Well I want to add a new line character at the end of each stockLine.

Therefore I concatenate it with the newline character.
@stockLines = @stockLines.to_s + event.get('stockInformation') + '\n'

But it concatenates \\n instead of \n. Therefore I was trying if I could substitute \\n with \n. I can't seem to do that anyway. I just want to concatenate a newline character at the end. Somehow \n is converted into \\n


In Ruby if you write a string with single quotes it will escape any \n.
You need to concatenate with "\n", eg:
@stockLines = @stockLines.to_s + event.get('stockInformation') + "\n"

(Saram Ali) #9

How can I do that in double qoutes, assuming the

code => tag opens with a double qoute too.


Use single quotes


You could either change the code => "<ruby code>" to use single quotes, and change all quotes in your ruby code to double quotes (code => '@stockLines = @stockLines.to_s + event.get("stockInformation") + "\n"; <other code>').
Or you could extract the ruby script to a separate file, and link to it with path => "/path/to/script.rb".

(system) closed #12

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