Hello, readers! I hope my previous posts on OpenCV face detection and JavaFX material design were useful to you. Here I will show you how to use the Linux utility such as grep for pattern matching using regular expressions. I will also show you some grep regular expression examples.
So let’s get started.
The Grep command
Before describing grep regular expression examples, a brief introduction to Grep utility. Grep(globally search a regular expression and print) is a command line UNIX utility for searching for patterns that match a regular expression in a plain text data set.
So, how to use grep?
The usage for grep : grep [‘regex’] [where_to_look]
so for example, if we have a file called success_story.txt and we have to find all the lines where the letter x occurs, we would run the following command.
grep 'x' success_story.txt
And similarly, for all those lines where x occurs in the beginning of a word, we will use the following command
grep '^x' success_story.txt
Note: A full stop does not mean the end of a line in Unix. There needs to be a new line character.
Switches in grep
There are certain switches that could be used with the grep command for different functionalities.
The usage of using switches with grep command is
grep -[switch] [‘regex’] [where-to-look]. Some of the commonly used grep command line switches are described in the table below.
|-i||ignore case while searching for the regex|
|-v||reverse the sense of the search|
|-c||only print the count of lines in which a match was found|
|-l||only print file names having matches|
|-L||print file name not having matches|
|-n||print line numbers of matching lines|
|-h||don’t print file names|
There are many more command lines switches available other than those described above. I suggest you see the man page of grep using the following command in your terminal.
Regex meta characters
Meta characters hold special significance in regular expressions. These meta characters are basically the building blocks of regular expressions. So a character may be a meta character with special meaning, or it may just be a regular character with literal meaning.
Here are the useful meta characters used in grep, and some of their usages are shown as grep regular expression examples
|^||Anchor the regex to the beginning of the line|
|$||Anchor the regex to the end of the line|
|\||matches what follows it (allows meta characters to be matched|
|||match only one within the set|
|[^ ]||^ at the beginning of a bracketed expression negates the expression|
|.||It matches a single character of any value, except end of line|
|*||matches zero or more of the preceding character or expression|
Some grep regular expression examples
Search for any single character that is in the set ‘Q, O, J’
grep [QOJ] success_story.txt
Search for any single character that is not in the set ‘Q, O, J’
grep [^QOJ] success_story.txt
So, similarly, you can try out all other grep regular expressions yourself in your Unix/Linux terminal.
I hope this tutorial provided a simple guide on how to use grep utility with some grep regular expression examples. Please bookmark this website for more content. Also, check out my other tutorial series on Oracle database concepts. Comments and suggestions are always welcome.