Search for text in Linux files

Sometimes you may need to find a file that contains a specific string or find a line in the file where there is a word. In Linux, all this is done using one very simple yet powerful utility – grep. With it you can search not only the time limits in the files, but also filter the output of commands, and much more.

In this tutorial, we’ll look at how text is searched for in linux files, detail the possible options for grep, and also give some examples of working with this utility.

What is grep?

The Grep command, (stands for global regular expression print) is one of the most popular commands in the Linux terminal that is part of the GNU project. First of all, because it is a very powerful utility that allows users to sort and filter text based on complex rules.

This allows you to apply it to solve various problems. The grep utility is mainly used to find strings matching a string in the text or the contents of files. It can also be used to search by pattern or regular expressions. With it it is convenient in a matter of seconds to find a file in the file system with the correct line, find the text in the file, or filter only a couple of necessary lines from the output of the command. Now let’s look at how to use the command.

Grep Syntax

The command syntax is as follows:

$ grep [options] template [filename …]

  • Options are additional parameters that specify various settings, search and output, such as the number of rows or the inversion mode.
  • A template is any string or regular expression that will be searched
  • The file and the command are the place where the search will be conducted. As you will see further, grep allows you to search in several files and even in a directory using recursive mode.

As you can see, grep can not only search in linux files, but it can also filter standard output, this is a very convenient function when you need to select only errors from logs or find the process PID in the numerous output of the ps utility.

Options

Let’s look at the most basic options of the utility, which will help you more efficiently search for text in grep files:

  • -b-  show the block number before the line;
  • -c-  count the number of occurrences of the pattern;
  • -h-  do not output the file name in the search results inside Linux files;
  • -i-  not case sensitive;
  • -l-  display only the names of the files in which the template was found;
  • -n-  show the line number in the file;
  • -s-  do not show error messages;
  • -v-  invert the search, output all the lines except those that contain the template;
  • -w-  search for a pattern, as a word surrounded by spaces;
  • -e-  use regular expressions when searching;
  • -An-  show the occurrence and n lines before it;
  • -Bn-  show the occurrence and n lines after it;
  • -Cn-  show n lines before and after occurrence;

All the most basic options have been considered, and even more, now let’s move to the examples of the grep linux command.

Examples of using

With the theory over, now let’s move on to practice. Consider a few basic examples of searching within Linux files with grep, which you might need in your daily life.

Search for text in files

In the first example, we will look for the User user in the Linux password file. To search for grep text in the /etc/passwd file, enter the following command:

As a result, you will get something like this, if, of course, there is such a user:

And now we will not take into account the register during the search. Then the combinations ABC, abc and Abc from the point of view of the program will be the same:

Print multiple lines

For example, we want to select all the errors from the log file, but we know that in the next post after the error can contain useful information, then with the help of grep we will display a few lines, we will look for errors in Xorg.log using the pattern “EE”:

Will print a string with an occurrence and 4 lines after it.

Output the target string and 4 lines before it

Output two lines at the top and bottom of the entry.

Regular expressions in grep

Regular expressions grep – a very powerful tool at times expanding the ability to search for text in files grep. To activate this mode, use the -e option. Let’s consider some examples:

Search for an entry at the beginning of a line with the help of the special character “^”, for example, we will display all messages for November:

Nov 10 01:12:55 gs123 ntpd[2241]: time reset +0.177479 s
Nov 10 01:17:17 gs123 ntpd[2241]: synchronized to LOCAL(0), stratum 10

Search at the end of the line, special character “$”:

Find all the lines that contain the numbers:

In general, regular expressions grep is a very extensive topic, in this article I just showed a few examples to let you know what it is. As you saw, thus, the search for text in grep files becomes even more flexible. But a full explanation of this topic requires a whole article, so for now let’s skip them and go further.

Recursive use of grep

If you need to search for grep text in several files located in the same directory or sub directories, for example, in Apache configuration files – /etc/apache2/ – use recursive search. To enable recursive search in grep, there is the -r option. The following command will look for text in Linux files in all the subdirectories /etc/apache2 for the occurrence of the string mydomain.com:

In the output you will get:

Here, before the found line, the name of the file in which it was found is specified. The output of the file name can be easily turned off with the -h option:

Word search in grep

When you search for the string abc, grep will also output kbabc, abc123, aafrabc32, and similar combinations. You can force grep to search for the contents of files in linux only those lines that turn off the search words with the -w option:

Search for two words

You can search the contents of the file for more than one word, but several. To search for two different words, use the egrep command:

Number of occurrences of a string

The Grep utility can tell you how many times a specific string was found in each file. To do this, use the -c (counter) option:

With the -n option, you can output the line number in which the entry was found, for example:

We get:

Inverted search in grep

The grep linux command can be used to find strings in a Linux file that do not contain the specified word. For example, output only those lines that do not contain the word pairs:

Output file name

You can specify grep to display only the file name in the cat, the specified word was found using the -l option. For example, the following command will output all file names, when searching for the contents of which the primary entry was found:

Colored output in grep

Also, you can force the program to highlight other occurrences of the output in the output:

It turns out:

conclusions

That’s all. We’ve covered everything about using the grep command to search and filter the output of commands in the Linux operating system. With proper application, this utility will become a powerful tool in your hands. If you have any questions, write in the comments!

Authored By Imran Yousaf

I am Imran Yousaf, a computer geek, founder of the site Smashinglab.com. I am a die hard fond of open-source software and Linux operating system. In addition to Linux, I am interested in everything related to information technology and modern science.

Leave a Reply

Your email address will not be published. Required fields are marked *