multiline filebeat pattern to match miltiple word - multiline

Some confusion here where I have to use filebeat multiline pattern to collec data.
Question is how to use multiple pattern ?
Here what i use now
multiline.pattern : '^Select'
So for above pattern we can see all word start from select will be match. So my question how about INSERT,UPDATE and DELETE word ?
Also one question can I use below pattern to indicate end of multiline match ?
multiline.flush_pattern: ';'
Any idea or help is highly appreciated

To your first question:
You can specify multiple words for the beginning of the message within a single regex. So if I understood you correctly, you want to include all log lines that start with Select, INSERT, UPDATE and DELETE. To achieve this you would define a group of valid values like so:
multiline.pattern : '^(Select|INSERT|UPDATE|DELETE)
The pipe-character ( | ) acts as an OR-Operator. Please note that by default regex is case sensitive. So e.g. messages that start with an uppercase SELECT would be ignored in the sample above.
To your second question:
Besides multiline.pattern you have to specify the settings multiline.match and multiline.negate:
multiline.match determines if the log lines before or after the pattern should be put into a single event.
multiline.negate determines if the following lines have to match the pattern.
So instead of specifying a particular end-character you tell Filebeat that every log line that matches the pattern AND is following that line should get aggregated UNTIL the following line matches again the pattern.
(See https://www.elastic.co/guide/en/beats/filebeat/current/multiline-examples.html for a full reference and description).
Example:
Assuming your log file is structured as following:
Select foo from bar\n where baz = 1\n and id =4711;\n\n
DELETE from bar\n where baz = null;\n\n
INSERT ...
the following config should do the job:
multiline.pattern : '^(Select|INSERT|UPDATE|DELETE)'
multiline.match: after
multiline.negate: true
I hope I could help you.

Related

MySQLAdmin replace text in a field with percent in text

Using MySQLAdmin. Moved data from Windows server and trying to replace case in urls but not finding the matches. Need slashes as I don't want to replace text in anything but the urls (in post table). I think the %20 are the problem somwhow?
UPDATE table_name SET field = replace(field, '/user%20name/', '/User%20Name/')
The actual string is more like:
https://www.example.com/forum/uploads/user%20name/GFCI%20Stds%20Rev%202006%20.pdf
In a case you are using MariaDB you have REGEXP_REPLACE() function.
But best approach is to dump the table into the file. Open it in a Notepad ++
and run regex replace like specified on a pic:
Pattern is: (https:[\/\w\s\.]+uploads/)(\w+)\%20(\w+)((\/.*)+)
Replace with: $1\u$2\%20\u$3$4
Then import the table again
Hope this help
If its MariaDB, you can do the following:
UPDATE table_name SET field = REGEXP_REPLACE(field, '\/user%20name\/', '\/User%20Name\/');
First, please check, what is actually stored in the database: %20 is a html-entity which represents a whitespace. Usually, when you are storing this inside the database, it will be represented as an actual whitespace (converted before you store it) -> Hence your replace doesn't match the actual data.
The second option that might be possible - depending on what you want to do: You are seeing the URL containing %20, therefore you created your database records (which you would like to fetch) with that additional %20 - And when you now try to query your results based on the actual url, the %20 is replaced with an "actual" whitespace (before your query) and hence it doesn't match your stored data.

MySQL select column string with LIKE or REGEX based on sub-string with delimiters

I have to compare a column value with a user input numbers. The string in the column is in the format 8|5|12|7|
Now, I need to compare a user input values 2,5,3 with this column value
When I use LIKE operator as '%2|%' I got the output by matching with column value |12|
How do I match the string by using Regular Expression or any other way?
If I understand the question correct, then to make sure that you get 2|.. or ..|2|.. or |2, you need to add or clauses
where col like '%|2|%'
or col like '2|%'
or col like '%|2'
or col='2'
Something similar to this to test for 2 in this example 12|8|12|5|12|7|2|12|22
# (^|\|)2(\||$)
#
#
# Match the regex below and capture its match into backreference number 1 «(^|\|)»
# Match this alternative (attempting the next alternative only if this one fails) «^»
# Assert position at the beginning of the string «^»
# Or match this alternative (the entire group fails if this one fails to match) «\|»
# Match the character “|” literally «\|»
# Match the character “2” literally «2»
# Match the regex below and capture its match into backreference number 2 «(\||$)»
# Match this alternative (attempting the next alternative only if this one fails) «\|»
# Match the character “|” literally «\|»
# Or match this alternative (the entire group fails if this one fails to match) «$»
# Assert position at the end of the string, or before the line break at the end of the string, if any (line feed) «$»
REGEXP "(^|\|)2(\||$)"
This allows for the column value to just be 2 or 2|anything or anything|2 or first thing|2|end thing.
By looking your column design, one of the way u can do is LIKE '%|2|%'
It is bad design to build "arrays" in a cell. Use a separate table.
Anyway, FIND_IN_SET() is a function that does the work a lot easier than a regexp. (But you have to use ',')

Replace not working

I have a text-file of data in key-value pairs that I have managed to convert to a format where the key-value pairs are all separated by an underscore between them, and the key is separated from the value by a colon. I thought this format would be useful for keeping spaces intact within the data. Here's an example with the data substituted for ~~~~~~~s.
_ID:~~~_NAME:~~~~~_DESCRIPTION:~~~~~~~_TYPE1:~~~~~~_TYPE2:~~~~~~ ...etc
I want to convert this to a MySQL script to insert the data into a table. My problem is there are nullable fields that aren't included in every record. e.g. A record has a _TYPE1: and may or may not have a _TYPE2:
... _DESCRIPTION:~~~~~~_TYPE1:~~~~~~_TYPE2:~~~~~~_ADDRESS:~~~~~~~ ...
... _DESCRIPTION:~~~~~~_TYPE1:~~~~~~_ADDRESS:~~~~~~~ ...
... _DESCRIPTION:~~~~~~_TYPE1:~~~~~~_ADDRESS:~~~~~~~ ...
... _DESCRIPTION:~~~~~~_TYPE1:~~~~~~_TYPE2:~~~~~~_ADDRESS:~~~~~~~ ...
... _DESCRIPTION:~~~~~~_TYPE1:~~~~~~_ADDRESS:~~~~~~~ ...
I thought to fix this by inserting _TYPE2: after every _TYPE1 without a _TYPE2:. Since there are only a few different possible types, I managed to select the _ after each _TYPE1:~~~~~~ without a TYPE2: following it. I used the following regex, where egtype is one example of a possible type:
(?<=_TYPE1:egtype)_(?!TYPE2:)
At this point, all I have to do is replace that _ with _TYPE2:_ and every field is present in every line, which makes it easy to convert every row to a MySQL insert statement! Unfortunately, Notepad++ is not replacing it when I click the Replace button. I'm not sure why.
Does anyone know why it wouldn't replace an _ with _TYPE2:_ using that particular regex? Or does anyone have any other suggestions on how to turn all this data into a MySQL insert script?
Regex
To do what you want, try this:
Find:
_TYPE1:[^_]+\K(?!.*_TYPE2)
Replace:
_TYPE2:
You can test it with your sample data and have it explained here.
Python Script plugin
As a side note, I don't think it's possible to convert your data into SQL insert statements with the use of one and only one regular expression, and while I see what you are trying to do by adding fake TYPE2, I don't think it is your best option.
So, my suggestion is to use Notepad++'s Python Script plugin.
Install Python Script plugin, from Plugin Manager or from the official website.
Then go to Plugins > Python Script > New Script. Choose a filename for your new file (eg sql_insert.py) and copy the code that follows.
Run Plugins > Python Script > Scripts > sql_insert.py and a new tab will show up the desired result.
Script:
columns = [[]]
values = [[]]
current_line = 0
def insert(line, match):
global current_line
if line > current_line:
current_line += 1
columns.append([])
values.append([])
if match:
i = 0
for m in match.groups():
if i % 2 == 0:
columns[line].append(m)
else:
values[line].append(m)
i += 1
editor.pysearch("_([A-Z0-9]+):([^_\n]+)", insert)
notepad.new()
for line in range(len(columns)):
editor.addText("INSERT INTO table (" + ",".join(columns[line]) + ") values (" + ",".join(values[line]) +");\n")
Note: I'm still learning Python and I've a feeling that this one could be written in a better way. Feel free to edit my answer or drop a comment if you can suggest improvements!
Example input:
_ID:~~~_NAME:~~~~~_DESCRIPTION:~~~~~~~_TYPE1:~~~~~~_TYPE2:~~~~~~
_ID:~~~_NAME:~~~~~_DESCRIPTION:~~~~~~_TYPE1:~~~~~~_TYPE2:~~~~~~_ADDRESS:~~~~~~~
_ID:~~~_NAME:~~~~~_DESCRIPTION:~~~~~~_TYPE1:~~~~~~_ADDRESS:~~~~~~~
Example output:
INSERT INTO table (ID,NAME,DESCRIPTION,TYPE1,TYPE2) values (~~~,~~~~~,~~~~~~~,~~~~~~,~~~~~~);
INSERT INTO table (ID,NAME,DESCRIPTION,TYPE1,TYPE2,ADDRESS) values (~~~,~~~~~,~~~~~~,~~~~~~,~~~~~~,~~~~~~~);
INSERT INTO table (ID,NAME,DESCRIPTION,TYPE1,ADDRESS) values (~~~,~~~~~,~~~~~~,~~~~~~,~~~~~~~);
try searching for (_TYPE1:)(\S\S\S\S\S\S)(_ADDRESS:)
and replacing with \1\2_TYPE2:~~~~~~\3
i tested in notepad++ with your data and it works
don't forget to change the Search Mode to regular expression.
to turn it into an INSERT script just keep using regular expression like i did above, and bracket which ever field you want and then replace with a \number whichever field and move them around it should be pretty simple manual labor, have fun.
for example search for your whole line here i am only doing DESCRIPTION,TYPE1,and TYPE2
search for using regular expression
(_DESCRIPTION)(:)(\S\S\S\S\S\S)(_TYPE1)(:)(\S\S\S\S\S\S)(_TYPE2)(:)(\S\S\S\S\S\S)
then replace with something like
INSERT INTO table1\(desc,type1,type2\)values\('\3','\6','\9'\); (in notepad++)
If this is a once-off problem then a two step process would work. First step would add a _TYPE2:SomeDefaultValue to every line. Step two would remove it from lines where it was not needed.
Step 1: Find what: $, Replace with: _TYPE2:xxx
Step 2: Find what: (_TYPE2:.*)_TYPE2:xxx$, Replace with: \1
In both steps select "regular expression" and un-select "dot matches newline". Also change xxx to your default value.

Extracting MySQL data within "tags" using regular expressions? [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Simulating regex capture groups in mysql
Good day,
I have many rows of data stored in a MySQL table. A typical value could look something like this:
::image-gallery::
::gallery-entry::images/01.jpg::/gallery-entry::
::/image-gallery::
Is there a way - by means of a regular expression that I can a) extract the term image gallery from the first line (it could be any phrase, not just image-gallery) and then extract the center line as two separate values like this:
gallery-entry and then images/01.jpg
There could be many lines of ::gallery-entry:: values, and they could be called anything as well. A more complete example would be:
::image-gallery::
::title::MY GALLERY::/title::
::date::2011-05-20::/date::
::gallery-entry::images/01.jpg::/gallery-entry::
::/image-gallery::
In essence I want this information: The content type (image-gallery) in the above case, first line and last line. Then I need the title as a key value style pair, so title as the key and MY GALLERY as the value. Then, subsequently, I would need all the rows of fields thereafter (gallery-entry) as key value pairs too.
This is for a migration script where data from an old system will be migrated over to a new system with different syntax.
If MySQL select statements would not work, would it be easier to parse the results with a PHP script for data extraction?
Any and all help is always appreciated.
Kind regards,
Simon
Try this regex:
::image-gallery::\s+::title::(.*?)::/title::.*?::gallery-entry::(.*?)::/gallery-entry::\s+::/image-gallery::
Use single-line mode (/pattern/s) so the .*? chews up newlines.
Your key-value pairs will be:
title: $1 (matching group 1)
gallery-entry: $2 (matching group 2)
From simulating-regex-capture-groups-in-mysql there does not seem to be a way to easily capture groups with a regex in mysql. The reason is that MySQL does not natively support capture groups in a regex. If you want that functionality you can use a server side extension like lib_mysqludf_preg to add that capability to MySQL.
The easiest way is to extract the whole column with SQL and then do the text matching in another language (such as php).
In my tests kenbritton's regex didn't work, but building off of it the following regex worked on your test data:
::image-gallery::\s+::title::(.*?)::\/title::\s+(?:.*\s+)*::gallery-entry::(.*?)::\/gallery-entry::\s+::\/image-gallery::

MySQL query for "starts with but may not fully contain"

Is there a way to do a MySQL query for data fields that start with but may not fully contain a given string?
For instance, if I had the following list of data items:
my_table
1. example.com
2. example.com/subpage
3. subdomain.example.com
4. ain.example.com
5. ple.com
I would like to feed
"example.com/subpage" and return #1, #2
"example.com" and return #1
"wexample.com" and return nothing
"exa" and return nothing
"subdomain.example.com/subpage" and return #3
Thanks a lot!
Given:
CREATE TABLE paths ( path VARCHAR(255) NOT NULL );
Searching for "example.com/subpage" would require the following query:
SELECT * FROM paths WHERE INSTR("example.com/subpage", path) = 1;
Just don't try to run it over a large dataset frequently...
Docs: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_instr
Since your test data indicates you don't want character-by-character matching (but something more like component by component), split the input into the components and search on all prefixes.
If you want to return results for example.com but not exam, you are NOT searching for something that "starts with" yuour input. Not sure if the question is wrong or the examples there.
If the examples are correct, you're going to need to do something to identify if your input is a URL or not using pattern matching like regex or at least specify some solid rules around what you want to match. You'll probably need to explain those rules before a correct recommendation can be made too.
It might be as simple as extracting anything before the "/" if there is one or using your application to break up your request to a url component and a path component.
Mode info on regex in mysql
It seems that you want the column value to match the start of your pattern:
SELECT * FROM my_table WHERE 'example.com' LIKE CONCAT(my_table.my_column, '%');
The downside of this is that it isn't going to use any indexes on my_column.