Logstash Remove '/' at the end of string if exists - logstash

I'm trying to remove '/' if exists from the end of a string, in Logstash using ruby for example:
'example/' -> 'example'
'other_example' -> 'other_example'
I tried:
url = evnet.get("[url]")
if url[-1] == '/'
url.slice!(-1)
end
But it didn't work, I believe I should write the "/" in a different way.
Thanks

Using event.get creates a local variable in the ruby filter, which your .slice then modifies. If you want to save that back to the event then you need to call event.set("[url]", url). That said, it might be easier to just
mutate { gsub => [ "url", "/$", "" ] }

Related

Reading an Array correctly

I have a json file called "GuideDB" which contains data about somethings.
GuildDB.json
{"GuideID":{"prefix":"","DJ":null,"Roles":""}}
Basically I want to access the data "Roles" in a way I can manage each element in that line, e.g.:
const myArray = GuildDB.Roles; //GuildDB.Roles is the only way to access that line.
myArray.forEach(element => {
console.log("<#&" + element + ">")
});
this code gives the error "forEach", because the array is not correctly formated.
How do I add Items in GuildDB.Roles?
client.database.guild.set(message.guild.id, {
prefix: GuildDB.prefix,
DJ: GuildDB.DJ,
Roles: GuildDB.Roles + ", " + createdRole.id, //Bug: First run always writes the comma first.
});
This will result the following:
{"GuideID":{"prefix":"","DJ":null,"Roles":", 123, 1234, 12345, 123456"}}
Which is not what I want.. and I don't care how this looks, I need to make it easy access to read/manage that data of Roles.
Hope anyone can help..
You can do something like this to convert Roles into array first and then use forEach loop on it.
const myArray = GuildDB.Roles.split(",");

search and replace a string after a match

I have a file that contains :
String url = "https://url_address/v2.0/vpc/peerings?name=change_variable"
I try to change the string change_variable with something else like
String url = "https://url_adress/v2.0/vpc/peerings?name=florian-testpeering-5"
But when I use :
sed 's/"https:\/\/url_adress\/v2.0\/vpc\/peerings?name/"https:\/\/url_adress\/v2.0\/vpc\/peerings?name=florian-testpeering-5"/'g
I Obtain :
String url = "https://url_adress/v2.0/vpc/peerings?name=florian-testpeering-5"=change_url"
What I did wrong ?
Edit :
To be more precise, I need to change the change_variable inside with a $peering who I declare before in my script.
The fact that you have forward slashes in the url, means that is better to use another character for the sed separator and so:
sed 's#String url = "https://url_address/v2.0/vpc/peerings?name=change_variable"#String url = "https://url_address/v2.0/vpc/peerings?name=florian-testpeering-5"#g'
The normal / has been changed for #, allowing for easier reading and processing of the url.
Is this what you're trying to do?
awk 'index($0,"https://url_address/v2.0/vpc/peerings?name=change_variable") { sub(/change_variable/,"florian-testpeering-5") } 1' file
String url = "https://url_address/v2.0/vpc/peerings?name=florian-testpeering-5"
If I understand your edit, and you are saying you have your string in a variable at the beginning of your script, similar to:
var='String url = "https://url_address/v2.0/vpc/peerings?name=change_variable"'
and you need to change the contents of the string replacing from name=... to the end with a new value, you can simply use bash parameter expansion with substring replacement, e.g.
var="${var/name=*/name=florian-testpeering-5\"}"
Now the variable var will contain:
String url = "https://url_address/v2.0/vpc/peerings?name=florian-testpeering-5"
You can do the same thing if the string you want to replace with is also stored in another variable, such as repl="lorian-testpeering-5", in that case your substring replacement would be:
var="${var/name=*/name=${repl}\"}"
(same result in var)

Logstash replace all json keys on filter

My logstash receives JSONs with some key name that contains '.' . I want to replace all dots with other string in all key names.
I found kv,json,mutate but it seems that there is no plugin that can iterate over every JSON key.
Is there any other plugin that could help beside writing code in Ruby plugin?
After a scan of the documentation, the only option I see for you is the gsub filter plugin. Code copied from the docs
filter {
mutate {
gsub => [
# replace all forward slashes with underscore
"fieldname", "/", "_",
# replace backslashes, question marks, hashes, and minuses
# with a dot "."
"fieldname2", "[\\?#-]", "."
]
}
}

Write json content in json format to a new file in Node js

Goal: To write the file content in json format using Node js. Upon opening the file manually, content should be displayed in json format
I tried both fs-extra module functions - outputJsonSync or writeFileSync to write json content to a file. They write the content inline as below
{"a":"1", "b":"2"}
However, I would like to see the content as below when I open the file manually:
{
"a" : "1",
"b" : "2"
}
I tried jsome and pretty-data on the data as follows:
fs.outputJsonSync(jsome(data))
fs.outputJsonSync(pd.json(data))
They also write data inline only with extra \ or \n and tabs added to the data but doesn't open in formatted style.
Any inputs are highly appreciated. Thanks!
[Update]
Other scenario:
const obj = {"a":"1", "b":"2"}
var string = "abc" + "splitIt" + obj
doSomething(string)
And inside the function implementation:
doSomething(string){
var arr = string.split("splitIt")
var stringToWrite = JSON.stringify(arr[1], null, ' ').replace(/: "(?:[^"]+|\\")*"$/, ' $&')
fs.writeFileSync(filePath, stringToWrite)
}
Following output is displayed when I open the file:
"[object Object]"
Once you have the object, you can specify a replacer function to separate each key-value pair by a newline, then use a regular expression to trim the leading spaces, then use another regular expression to insert a space before the : of a key-value pair. Then, just write the formatted string to a file:
const obj = {"a":"1", "b":"2"};
const stringToWrite = JSON.stringify(obj, null, ' ')
// Trim leading spaces:
.replace(/^ +/gm, '')
// Add a space after every key, before the `:`:
.replace(/: "(?:[^"]+|\\")*",?$/gm, ' $&');
console.log(stringToWrite);
Though, you may find the leading spaces more readable:
const obj = {"a":"1", "b":"2"};
const stringToWrite = JSON.stringify(obj, null, ' ')
// Add a space after every key, before the `:`:
.replace(/: "(?:[^"]+|\\")*",?$/gm, ' $&');
console.log(stringToWrite);
JSON.stringify, has two optional parameters, the first one being a replacer function, the second one(What you want) is for spacing.
const obj = {"a":"1", "b":"2"}
console.log(JSON.stringify(obj, null, 2))
This will give you:
{
"a": "1",
"b": "2"
}

Part of string search and replace in Node JS

I wanted to do some regex search on strings and remove that part of the string that matches.
Eg. I have following strings
xyz-v1.0.0#
abc-v2.0.0#
def-v1.1.0#
So, for such cases, I wanted to remove -v1.0.0#, -v2.0.0# and -v1.1.0# from these strings.
So, for this what regex can I use and how can I remove them in Node JS?
You can do this
.replace(/-.*$/, '') will check for -{anything} at the end of the string and replace with nothing.
const strs = [
'xyz-v1.0.0#',
'abc-v2.0.0#',
'def-v1.1.0#'
];
const newStrs = strs.map(str => str.replace(/-.*$/, ''));
console.log(newStrs);

Resources