Some examples of using the grep command, Linux basics course Tartu

Some examples of using the grep command, Linux basics course Tartu

Articles

It is impossible to imagine serious work with Linux without using output filtering. Because such a function allows you to save time significantly. And significantly simplifies routine tasks in the field of system administration. Today we will tell you about several useful examples of using the command grep - this is included in Linux Basics Course Tartu. And it is guaranteed to help you both during the interview and after employment in the IT industry.

Displaying adjacent lines in filter results, Linux Basics Course Tartu

Most likely, our regular readers already have an idea of ​​how the utility works. grep. However, for those who have only recently joined our company, we will tell you in a few words. This utility allows you to display the output lines / file corresponding to the desired pattern. And this is very convenient, as it simplifies the search for the necessary information. Such use is included in Basic Linux Course Tartu.

However, context is often needed to better understand the data. Fortunately, the developers have provided the ability to display, in addition to the lines adjacent to those in which a match with the searched regular expression was found. To do this, enter the command grep with option -C and after the space a number. In this case, the command will show the specified number of adjacent lines.

If you are interested in the lines that follow the pattern you are looking for, enter the option -A and a number. If you need the lines preceding the desired pattern, then replace the option with -B and specify their number. This simple technique significantly expands the possibilities of using the command to find the necessary information.

Using inverted search conditions in grep

It is worth noting that in some cases you will not know exactly what you are looking for in the text of a file or program output. However, you will know exactly what data you do not need. Let's say that for work you need to analyze the contents of a large settings file. Most of which consists of commented lines with explanations.

To make the text shorter and remove all unnecessary, it is enough to apply inversion of search conditions. And set the symbol " as the search pattern#» («lattice»). In this case, to invert the conditions, it is worth adding to the command grep an option -v. The main thing is to remember to put the required template in quotes. As a result of executing this command, all lines with comments will be hidden. And only lines with parameters will remain in the output. To learn more, we recommend that you go through our Red Hat Linux courses Tartu.

Recursive search through all directories using grep

Another important technique for using the command grep — is a recursive search through the contents of a directory with subdirectories. Let's take a closer look at an example of using this function. Enter the command in the command line grep with option -r and add two arguments to it:

  • first - the required template 'sudo';
  • then - the directory to start the recursive search ~/.

As a result, the utility will check all the directories in your home directory and the subject of the content of the sought pattern in the files. Most likely, it will find matches in hidden file .bash_history. Contains the history of command usage in your account. As a result, you will see all examples of command usage sudo recently. Agree, it's quite convenient.

Most likely, you will need other examples of using this interesting utility in your work. To learn more and become a specialist, we recommend signing up for Linux Lecture Course Tartu. Moreover, it is possible to pass it on the platform SEDICOMM University online. According to an individual schedule, at your own pace and with a comfortable level of load.

Our team SEDICOMM University: Cisco Academy, Linux Professional Institute, Python Institute.