Explain the grep command in shell

Time:2020-4-2

Search text files with ‘grep’

If you want to find a string in several text files, you can use the ‘grep’ command. ‘grep’ searches the text for the specified string. For example, suppose you are searching for a file with the string ‘magic’ in the ‘/ usr / SRC / Linux / documentation’ Directory:


$ grep magic /usr/src/linux/Documentation/*
sysrq.txt:* How do I enable the magic SysRQ key?
sysrq.txt:* How do I use the magic SysRQ key?

The file ‘sysrp. TXT’ contains this string, which discusses the function of sysrq.

By default, ‘grep’ searches only the current directory。 If there are many subdirectories under this directory, ‘grep’ will be listed as follows:


grep: sound: Is a directory

This may make the output of ‘grep’ hard to read. There are two solutions:

Explicitly ask to search for subdirectories:grep -r

Ignore subdirectories:grep -d skip

Of course, if you expect a lot of output, you can do it through the pipeline

Turn it to ‘less’ to read:


$ grep magic /usr/src/Linux/Documentation/* | less

In this way, you can read more easily.

It should be noted that you must provide a file filtering method (if you want to search all files, *). If you forget, ‘grep’ will wait until the program is interrupted. If you are in a situation like this, press Ctrl + C and try again. (important!!)

Here are some interesting command line arguments:

Grep – I pattern files: case insensitive search. The default is case sensitive,
Grep – L pattern files: only matching file names are listed,
Grep – L pattern files: List mismatched file names,
Grep – W pattern files: matches only the whole word, not a part of the string (for example, ‘magic’, not ‘magic’),
Grep – C number pattern files: the matching context displays [number] lines respectively,
Grep pattern1 | pattern2 files: displays rows matching pattern1 or pattern2,
Grep pattern1 files | grep pattern2: displays rows that match both pattern1 and pattern2.

There are also special symbols for searching:

\< and \ > mark the beginning and end of the word respectively.

For example:

Grep man * will match 'Batman', 'manic', 'man', etc, 
Grep '\ < man' * matches' manic 'and' man ', but not' Batman ', 
Grep '\ < man \ >.

‘^’: refers to the matching string at the beginning of the line,

‘$’: refers to the matching string at the end of the line,

If you’re not used to command-line arguments, try “grep” in the graphical interface, such as rexgrep. This software provides and, or, not syntax, and beautiful buttons: -). If you just need clearer output, try fungrep.

Grep’s common command syntax

1. Double quotation mark reference and single quotation mark reference

When entering string parameters in the grep command, it is best to enclose them in double quotes. For example: “m y s t r i n g”. There are two reasons for this. One is to avoid being misunderstood a s the s h e l command. The other is to find a string composed of multiple words, such as “jet plane”. If you don’t enclose it in double quotes, the word p l a n e

It will be mistakenly considered as a file, and the query result will return the error message “file does not exist”.

When calling variables, you should also use double quotation marks, such as: g r e p “$m y VA R” filename, if not, no result will be returned.

When calling pattern matching, you should use single quotation mark. [root @ MyPC] ා echo ` grep 123 111. TXT ‘(# note that it is the back single quotation mark)

2. The common options of grep are:

-C. output only the count of matching rows.
-I. case insensitive (single character only).
-H. do not display the file name when querying multiple files.
-L. when querying multiple files, only the file name containing matching characters will be output.
-N display matching lines and line numbers.
-S does not display error messages that do not exist or have no matching text.
-V displays all lines that do not contain matching text.

3. Special – query in multiple files

$grep "sort" *. D o C (ා find the string "s o r t" in all. D or C files in the current directory)
 $grep "sort it" * (ා or query the word "sort it" in all files)

All the following examples refer to queries in a single file

4. line matching

$ grep -c “48″ data.f
$4 (ාg r e p returns the number 4, meaning that there are four lines containing the string "48". )
$grep "48 ″ data. F (񖓿 display 4 lines of text containing" 48 ″ string)

5. Display all rows that meet the matching pattern:


[[email protected] oid2000]# grep -n 1234 111.txt
1:1234
3:1234ab

6. Exact match


[[email protected] oid2000]# grep “1234\>” 111.txt
1234

7. Query the empty row, and query the row beginning or ending with a certain condition.

Use a combination of ^ and $to query for empty rows. Use the – n parameter to display the actual number of rows

[root @ MyPC oid2000] ා grep - n "^ $" 111.txt (return result 2: ා indicates that the second line is empty)
[root @ MyPC oid2000] ා grep - n "^ ABC" 111.txt (ා query lines starting with ABC)
[root @ MyPC oid 2000] ා grep - n "ABC $" 111.txt (ා query lines ending in ABC)

8. Match special characters and query characters with special meanings, such as $. ” * [] ^| \ +?, which must be preceded by \.

[root @ MyPC oid2000] ා grep "\." 111.txt (ා query all lines containing ".." in 111.txt)
[root @ MyPC oid2000] ා grep "my \. Conf" 111.txt (ා query the line with the file name my. C o n f)

9. Directory query

[root @ MyPC oid2000] ා LS - l| grep "^ D" ("if you want to query the directory in the directory list)
[root @ MyPC oid2000] ා LS - l| grep "^ D [D]" (ා query all files in a directory that do not contain a directory)
[[email protected]]# ls -l |grpe “^d… . X.. X "(ා query the directory collection of other users and user group members with executable rights)

The above is the grep command in the shell that Xiaobian introduced to you. I hope it can help you. If you have any questions, please leave me a message, and Xiaobian will reply you in time!