Virtual Lab : Get Your hands dirty with grep & RegEx
Other Learning Articles that you may like to read
Free Courses We Offer
Paid Training Courses we Offer
Regular expressions are extremely useful in extracting information from text such as code, log files, spreadsheets, or even documents. As an unix administrator we do regularly use the regex to perform various problem diagnosis , automation or remediation tasks. And it is almost impossible to imagine the life of unixadmin without RegEx usage. This quick virtual lab session will give you the basic usage knowledge of regular expressions with grep, which is a essential skill required for unix system administrators.
Below is the Learning Map, that makes you comfortable with Grep and RegEx ( i.e. Regular Expressions) within a day:
Step 1 : Understand how do we search lines that matches particular words / numbers.
The first thing to recognize when using regular expressions is that everything is essentially a character, and we are writing patterns to match a specific sequence of characters (also known as a string). Characters include normal letters, but digits as well. In fact, numbers 0-9 are also just characters and if you look at an ascii table, they are listed sequentially.
Step 2 : Understand how to work with “.” ( dot) while extracting data.
In regular expressions, we are often matching pieces of text that we don’t know the exact contents of, other than the fact that they have some pattern (eg. are phone numbers for example). In such cases we do use the dot as wildcard that can represent any character in a specific position of the word
Step 3 : Understand how to match the words that have specific characters.
When you use the dot (.) as wild card, that will match to any character in the given position, it is useful but sometimes it is too powerful. Just incase if we want to match limited set of characters in a specific position we should go different kind of RegEx pattern.
Step 4 : Understand how to exclude words with specific characters.
When we have big list of matching characters that we are looking in a word our RegEx will become lengthier and complex. In such cases, we can use RegEx to specify the characters that we dont want to see instead of mentioning big list of characters that we want to see.
Step 5 : Understand how to use Character Ranges, instead of using individual character list , while matching the words.
If we want to match or exclude a large number of sequential characters? eg. Matching a…m but not n…z. Do we have to write [abcdefghijklm]? Not really required we can simply use the character ranges.
Step 6 : Understand how to match words with arbitrary number of characters.
It is tricky to match the words, when they have arbitrary number characters ( that means some of the characters repetitive for one or more number of times.
Step 7 : Understand how to retrieve data conditionally.
So far whatever we understand is finding the words either matching or excluding specific characters. Sometime we might require to match the lines using group of words, in such cases RegEx goes well with the logical operators.
Step 8 : Understand how to match the words with optional characters
In Step 6 we learnt about matching words with the characters which are repeated one or more number of times, but it is often required to match the words which might be having optional characters ( i.e. zero or more number of times)
Step 9 : Understand how to deal with the white spaces.
When dealing with real-world input, such as log files and even user input, it’s difficult not to encounter white spaces We use it to format pieces of information to make it to read and scan visually, and a single space can change the entire output that we are looking for.
Step 10 : Understand how to match the lines with specific characters either at the starting or the ending.
Sometimes it is not desirable to match the words/patterns from any where of the text, instead we want to search the patterns only either beginning or ending of the each line.