Grep is another useful console utility that is very popular. It is easy to use, and at the same time has a wide flexibility and capabilities. The “grep” command in GNU/Linux comes with all distributions by default. The utility searches for parts of the text inside other texts without opening them according to the selected template (in particular, regular expressions).
The program is not limited only to working with files. It copes well with the text in the terminal, processing the output data of other utilities through the pipe | symbol. This article describes how grep works, it’s syntax, and practical examples.
Syntax and Parameters of Grep Command
The general view of the grep command when working with a file looks like this:
grep [options] pattern file
If you do not specify the file parameter, the program will receive data from the standard input stream (that is, from the keyboard) line by line. To exit this mode, send a process interruption signal using the key combination Ctrl + C.
When processing the output from another grep utility, use this:
previous commands | grep [options] pattern
As you can see, the required attribute for grep to work in both cases is pattern. It determines by what rule the text will be searched.
Below is a table of frequently used options in short and long forms:
|Short (BSD) option||Long (GNU) option||Value|
|-b||–byte-offset||shows block numbers at the beginning of a line|
|-c||–count||output of the number of matches to the pattern|
|-l||–files-with-matches||only displays file names where there was a pattern match|
|-n||–line-number||shows the line number in the file|
|-s||–no-messages||does not display error messages|
|-v||–invert-match||display all results that do not match the pattern|
|-w||–word-regexp||define a pattern as a word separated by spaces on the left and right|
|-e PATTERN||–regexp=PATTERN||use specified regular expression to search|
|-E ‘PATTERN’||–extended-regexp=’PATTERN’||use specified extended regular expression|
|-A N||–after-context=N||show match and N lines after it|
|-B N||–before-context=N||show match and N lines before it|
|-C N or -N||–context=N||show match and N lines around it|
Examples of using Grep Command in Linux
The following are common tricks for using the grep utility.
Search for Text in Files
Find the mention of anthony in /etc/passwd
grep anthony /etc/passwd
It is worth noting that the utility is case-sensitive by default in the pattern.
Multiple Line Output
For example, an analysis of an error log may be more productive if you’ll find out the contents of several lines after a pattern match is found.
grep --after-context=2 --ignore-case "error" /var/log/mysql/error.log
Regular Expression Search
This powerful tool allows sophisticated search, giving you an ability to specify more complex patterns according to some strict rules. The regular expression language is fairly common, but learning it takes some time.
Find all lines in the log made at the eleventh hour in which warning or error occurs:
grep -e 10: /var/log/mysql/error.log | grep -E '(Warning|Error)'
Sometimes it is necessary to find the specified word as a separate part of the sentence, and not as a part of another word.
For example, we’ll find the letter ‘to’ in the text.txt file: first as an entry, and then as a separate word.
Find information about all users in the /etc/passwd file whose shell is not located in /usr/sbin/:
grep -v 'sbin' /etc/passwd