UNIX has some very powerful tools in it's arsenal in the form of it's commands. Most of these are all inherited by Linux making it a powerful free and open source operating system. One of these tools that can get quite handy is "grep" command.
Grep is short for "Global Regular Expression Print". It was developed by Ken Thompson for UNIX operating system. He also created B programming language that is a direct predecessor to C programming language. He, along with Dennis Ritchie, was also involved in the creation of C programming language and UNIX operating system.
If you have read my first blog, then you would know that I am fairly new to Linux, however I did learned Unix as a part of my curriculum back in the collage days. Even back then, I wasn't very good in using Unix commands. Now that I have started using Linux, I've got an opportunity to explore the terminal side of this OS. Knowing UNIX/Linux is quite a vital skill in the IT Industry. Even my current job involves some use of UNIX.
Since I have not been that good in using some of the powerful tools like grep, I had to manage by using the simple commands that I do remember, like ls, mv and cd commands, but one day, I was stuck in a situation, and a friend of mine at work, who is pretty good in UNIX, helped me out. I saw him using grep command that I didn't remember back then. After all, the last time I used it was 6-7 years back. I was quite impressed by the way he used the command line. Now that I have Linux OS, I thought of replicating that use of grep command in my personal machine.
So, now that I was ready to replicate what my friend did back on that day, I opened the terminal and realized something. I didn't even remember what "grep"command is. So, I decided to learn from the web. I opened Youtube app in my phone and searched "grep command tutorials", and I found several of them. I watched the video available in "ProgrammingKnowledge" YouTube channel. Here is the video.
This video was quite helpful to me. Now I tried using what I learned. I found that I was doing something wrong, it was not showing any results i.e. the output was blank. So, I checked the syntax in the web, and found my mistake. I was trying to use something like the below.
| grep abc abc.txt |
Apparently, the correct command was the below one.
| grep "abc" abc.txt |
Now that I got that one right, it was time to explore this command. To do so, the first thing I did was that I exported the manual for ls command in a txt file and then tried to search the option "-l". It took some time, but I figured out how to do that and then tried it with some of the "grep" options.
Now, you may ask "How is this command useful to us as programmers and developers?". To answer that, let's take a scenario. While working on a Java project, you declared an "int" variable in a source file, declaring it as public. Now you used this variable in different source files. At a later stage, you realized that the operations in which the variable is getting used needs it to be a double variable instead of an int variable. But, since it's been quite some time since you created that variable, you might not remember in exactly with source file you declared it. Now you can do one of two things. Either you can pick a source file that is using that variable and then reverse engineer to find out the required file. But, it may take some time, and in case of big project, it may become practically unfeasible to do that. So, you can use the second method, which is, using "grep" command to find out "int <variable name>". Using grep command, you can find the file name(s) that contains the variable declaration for the required variable with line number. It can also search the whole tree of directories, so if the project has the source files arranged in different directories as per requirement(i.e. the project follows a particular directory structure), then too it can find out the required file. In fact, "grep" command can also prove to be helpful for debugging purposes.
Now that I learned using "grep" command, it was time to take it to next level. There has been times when I needed to find out a file but did not know the exact name and/or case of the file. Of course, ls command can handle the name part by the wild card character *, but what about the case? For example, if I want to search the file abc_*.txt, but then there could be files by the name abc_123.txt, aBc_456.txt, ABc_789.txt, etc. If I search abc_*.txt using the ls command it will only find the first file. So I thought, "can I use ls and grep command in combination to make this possible? Let's give it a try". I did remember that pipe(|) can be used in order to use more that one command in combination, but didn't remember the correct order of these commands. It took a couple of trials but I eventually found the correct one. For the record, below is the correct one.
| grep -i abc_*.txt | ls -ltr |
While trying to get to this final command, I found something. I found how important UNIX/Linux and is to us and that grep is really a secret weapon of UNIX/Linx operating system that surpasses it's successor aka standard search function of GUI.
Comments
Post a Comment