Detailed explanation of using grep command in Linux

Detailed explanation of using grep command in Linux

Linux grep command

The Linux grep command is used to search for strings that meet the criteria in files; it can also be used to search for files whose content contains a specified template style. It can use regular expression search to search for specified string patterns in files, list file names containing substrings matching the pattern, and output text lines containing the string.

The way grep works is this: it searches for a string pattern in one or more files. If the template contains spaces, it must be quoted and everything after the template is treated as a file name. The search results are sent to standard output without affecting the original file content.

Basic syntax:

grep [options] pattern [files]

The main parameters of [options] are:

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

-A<display line number> or --after-context=<display line number>: In addition to displaying the column that matches the template style, it also displays the content after that line.

-b or --byte-offset: Before displaying the line that matches the pattern, mark the number of the first character in the line.

-B<display line number> or --before-context=<display line number>: In addition to displaying the line that matches the style, it also displays the content before that line.

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

-C<display line number> or --context=<display line number> or -<display line number>: In addition to displaying the line that matches the style, it also displays the content before and after that line.

-d <action> or --directories=<action>: This parameter must be used when the directory to be searched is specified instead of the file, otherwise the grep command will report information and stop the action.

-e<template pattern> or --regexp=<template pattern>: Specify a string as a pattern for searching file contents.

-E or --extended-regexp: Use the extended regular expression pattern.

-f<rule file> or --file=<rule file>: Specify a rule file whose content contains one or more rule patterns, allowing grep to search for file contents that meet the rule conditions, with one rule pattern per line.

-F or --fixed-regexp : Treat the pattern as a list of fixed strings.

-G or --basic-regexp: Use the pattern as normal notation.

-h or --no-filename: Do not indicate the file name to which the line belongs before displaying the line that matches the pattern.

-H or --with-filename: Before displaying the line that matches the pattern, it indicates the name of the file to which the line belongs.

-i or --ignore-case: Ignore differences in character case.

-l or --file-with-matches: List the names of files whose contents match the specified pattern.

-L or --files-without-match: List the names of files whose contents do not match the specified pattern.

-n or --line-number: Before displaying the line that matches the style, mark the column number of the line.

-o or --only-matching: Display only the matching PATTERN part.

-q or --quiet or --silent: Do not display any information.

-r or --recursive: The effect of this parameter is the same as specifying the "-d recurse" parameter.

-s or --no-messages: Do not display error messages.

-v or --revert-match: Display all lines that do not contain matching text.

-V or --version: Display version information.

-w or --word-regexp: Only display columns that match whole words.

-x --line-regexp : Show only columns that fully match the specified line regexp.

-y: This parameter has the same effect as specifying the "-i" parameter.

-?: Display the ? above and below the matching line at the same time. For example, grep -2 pattern filename displays the two lines above and below the matching line at the same time.

The main parameters of pattern regular expression are:

\: Ignore the original meaning of special characters in regular expressions.

^: Matches the beginning of a line of the regular expression.

$: Matches the end of the regular expression.

\<: Start at the line that matches the regular expression.

\>: To the end of the line matching the regular expression.

[]: A single character, such as [A], which means A meets the requirements.

[ - ]: Range, such as [A-Z], which means A, B, C to Z all meet the requirements.

.: All single characters.

*: contains characters and can be 0 in length.

Code example:

Example 1: In the current directory, find the files with the prefix "test" that contain the string "test" and print out the lines containing the string. In this case, you can use the following command:

grep test test*

Output:

testfile1:This a Linux testfile! #List the lines in testfile1 that contain the character testtestfile_2:This is a linux testfile! #List the lines in testfile_2 that contain the character testtestfile_2:Linux test #List the lines in testfile_2 that contain the character test

Example 2: Reverse search. The "-v" parameter can be used to print out the content of the lines that do not meet the conditions. Find the line that does not contain test in the file whose file name contains test

grep -v test *test*

Output:

testfile1: helLinux! 

testfile1:Linis a free Unix-type operating system. 

testfile1:Lin 

testfile_1:HELLO LINUX! 

testfile_1:LINUX IS A FREE UNIX-TYPE OPTERATING SYSTEM. 

testfile_1:THIS IS A LINUX TESTFILE! 

testfile_2:HELLO LINUX! 

testfile_2:Linux is a free unix-type opterating system.

You may also be interested in:
  • Detailed explanation of grep and egrep commands in Linux
  • Grep command to match tabs and newlines in Linux
  • Linux uses grep to filter multiple conditions and grep common filtering commands
  • How to retrieve file contents using grep command in Linux
  • Common commands and differences between find and grep in Linux
  • Introduction to Linux text filtering grep basic commands (5)
  • Detailed explanation of Linux grep command
  • In-depth understanding of grep command in Linux
  • Grep command usage in Linux
  • Detailed explanation of the usage of grep command in Linux

<<:  Detailed steps to install Mysql5.7.19 using yum on Centos7

>>:  MySQL high availability solution MMM (MySQL multi-master replication manager)

Recommend

How to use Portainer to build a visual interface for Docker

Portainer Introduction Portainer is a graphical m...

Implementation of TCPWrappers access control in Centos

1. TCP Wrappers Overview TCP Wrappers "wraps...

Vue implements multiple selections in the bottom pop-up window

This article example shares the specific code of ...

HTML table markup tutorial (4): border color attribute BORDERCOLOR

To beautify the table, you can set different bord...

A brief discussion on the execution details of Mysql multi-table join query

First, build the case demonstration table for thi...

Bugs encountered when using mybatis-generator with mysql8.0.3 in IDEA

1. Add the plug-in and add the following configur...

React implements double slider cross sliding

This article shares the specific code for React t...

What to do if you forget the initial password when installing MySQL on Mac

Forgetting the password is a headache. What shoul...

How to reset your Linux password if lost

1. The startup menu is to move the cursor to the ...

Shorten the page rendering time to make the page run faster

How to shorten the page rendering time on the bro...