pyGrok library














































pyGrok library



PYTHON INTRODUCTION OF PYGROK LIBRARY

WHAT IS GROK?

Grok is a software with which we can extract data from a structured or an unstructured data using regex expressions. Basically, It is used to extract or obtain data from a data that maybe in form of long sentences, numbers or a mix of both. This software is mostly used to get information from logfiles, text messages, etc..

USES OF GROK?

Grok is used for:
1. Extract data by matching patterns in a string.
2. Used at relieving or dealing with complex regular expressions.
3. Getting information from raw data or structured data.

WHAT IS REGEX?

Regex refers to regular expressions. It contains few "Metacharacters" which on grouping form regular expressions.
Some metacharacters are:
1. [] - denotes a set of characters between the range mentoined.
2. .  - It replaces any character in the expression.
3. ^  - denotes the string or character to start with.
4. $  - denotes the string or character to end with.

WHAT IS PYGROK?


pyGrok is a python library which allows the Grok functionalities in python programs.

HOW TO INSTALL PYGROK?


To install pyGrok in your system, Open your terminal or command prompt and type :

pip install pygrok

This install pyGrok library in your system.

FIRST PROGRAM WITH PYGROK




In these program,

1. We first imported Grok package from pygrok library.
2. Then next we are taking input text from the user and storing it in the text variable.
3. After that, we are taking pattern for the text received and storing it in the pattern variable.
4. Now, we are creating a Grok object with the pattern we had and storing it in grok variable.
5. At last, we are matching text with match method with Grok object created.

This Outputs,




As you can see, We entered text "My name is Robert" and passed pattern "My name is %{WORD:name}" we get an output with a dictionary with the variable in pattern and assigned a value with the name in the text. The type WORD tells that the variable takes type string.

In the previous program we have only matched and extracted patterns of type strings. We can even take the matched patterns of type integer and float by putting an appropriate type name at the end of the string.

EXTRACTING NUMBERS FROM THE DATA



In this program we are not taking the pattern at runtime. Here, We are using NUMBER keyword (int, float) to get values as integer type and floating point type.
This program outputs an dictionary with currency and amount as key.

OUTPUT OF THE PROGRAM:



As you can see, The program returned a dictionary as output with keys currency and amount and values assigned to them in the text sentence. currency = 'rupees' and amount = 10000. And to notice that rupees is of type string and amount is of type integer.


More Articles of bhanu prakash sakkuri:

Name Views Likes
PYTHON INTRODUCTION TO NNTPLIB MODULE 245 0
PYTHON INTRODUCTION TO PYGROK LIBRARY PART-2 327 0
pyGrok library 819 1

Comments