Skip to main content

GREP - The secret weapon of UNIX/Linux

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

Popular posts from this blog

Swap two numbers without using a temporary variable

It's been a while since I've posted any blogs and vlogs. I've been trying to record videos for Planet of Codes Youtube channel, but there's quite a lot of noise whenever I try to record, so I thought why not I communicate through these blogs instead till I am able to record something for Youtube. So here we are, blogging for something that I actually tried to record in the form of a video. Usually in high schools and colleges, when most of us initially learn programming, we get a programming problem to swap two numbers. Usually the solution looks something like the below. num1 = 32 num2 = 87 temp = num1 num1 = num2 num2 = temp print(num1) print(num2) Note: Usually institutions ask students to write this program in C or C++ in first semester, however the above program is in Python, because, It's the logic that matters. This program can be written in any other programming language too. Well, here we are using a temporary variable "temp".  This problem can ac...

Significance of Development and Production Environment for any Application

The concept of the environment that any application runs on was something that I was unaware of as a student. I got introduced to these concept after becoming a part of corporate IT sector. Two of the most important environment are Development environment and Production environment. Of course, there are testing environments like  OAT and UAT environment, but at times these are taken as a sub part of the above mentioned major environments. So now the question arises, "What are  these environment? and why do I need to know about these?". Well, although subconsciously, we all are aware of these, not all of us are aware of how important these are to us and to each other. Development environment is basically the environment where the application is developed and maintained. Any changes that needs to be made to any application are made in development environment as well. The application in development environment could be any application made from scratch or any third party applica...

Project Narutopedia Webscraper

Hello Guys, It's been a while since I have posted something, isn't it?  So, what's up you ask? Well, it happened on Monday, 22nd February, 2021. I was just passing my time, watching a Youtube video on a virtual assistant project. The video was quite impressive to be honest. They even used some package to extract data from Wikipedia. After watching that video, I thought to myself, is there any such package for Narutopedia? Now those of you who know me personally, know how big of a Naruto and Boruto fan I am, so I started to check if I could find one, however, I couldn't find any. I am not saying that there is no Python package for Narutopedia, there could be one, it's just that I couldn't find any. So I thought, "Why not make a simple app that can extract data from Narutopedia for anything we search?". The only problem back then was that I had no idea how to do that. I then started to research and it took only a few minutes until I realized that there i...