In this tutorial we will show you how to use Unix Grep command.
GREP stands for Global Regular Expression Print and is used for filtering the searches and finding what you are looking for. This makes the search for different files existing on your system easy. Amongst unix commands grep is one of the easiest to use and work with.
To understand how you can use the grep command, follow the steps given below.
Step 1 – List the contents
Let’s start by first listing the contents of the directory. To understand the concept of the grep command, we will first display the contents of this text file here.
Step 2 – Search for a specific word in the text file
With that done, we will search for a particular word in the text file. For that, type grep space “the search query” followed by the name of the text file.
With that done, you will notice that the search query will be shown over here and it will automatically highlighted by red color.
This way you can use the unix grep command to search for a specific text file that you need.
Step 3 – Use the “-c” attribute
If you want a simple count of how many times the search query is found, you can use the -c attribute over here.
Once we hit enter, you can see that the number of results are shown on the screen.
And that’s it, from the above steps it can be seen that using the unix commands of grep are fairly easy and not at all time consuming. Therefore, to search a file in Linux, you can use this method and technique.