Home > Operation and Maintenance > Linux Operation and Maintenance > How to use grep command in linux? (detailed explanation)

How to use grep command in linux? (detailed explanation)

青灯夜游
Release: 2021-04-22 15:24:57
Original
22802 people have browsed it

The grep command in Linux system is a powerful text search tool. It can use regular expressions to search text and print out matching lines. Its usage syntax is "grep [options] pattern [files] ".

How to use grep command in linux? (detailed explanation)

#The operating environment of this article: linux5.9.8 system, Dell G3 computer.

Linux grep command

The Linux grep command is used to find qualified strings in files; it can also be used to find files containing specified template styles. It can use regular expression search to search for a specified string pattern in a file, list the file names containing substrings matching the pattern, and output the text lines containing the string.

grep works like this: it searches one or more files for a string template. If the template contains spaces, it must be quoted, and all strings following the template are treated as file names. The search results are sent to standard output without affecting the original file content.

Basic syntax:

grep [options] pattern [files]
Copy after login

Main parameters of [options]:

-a or --text: Do not ignore Binary data.

-A or --after-context= : In addition to displaying the column that conforms to the template style, and display the content after the row.

-b or --byte-offset: Mark the number of the first character of the line before displaying the line that matches the style.

-B or --before-context= : In addition to displaying the line that matches the style, the content before that line is displayed.

-c or --count : Count the number of columns that match the style.

-C or --context= or - : In addition to displaying the line that matches the style, and display this line Before and after content.

-d or --directories= : This parameter must be used when specifying directories instead of files to be searched, otherwise the grep command will report information and stop. action.

-e