Detailed explanation of grep COMMANDS IN SHELL

Source: Internet
Author: User
Use 'grep' to search for text files
If you want to find a string in several text files, you can use the 'grep' command. 'Grep' searches for the specified string in the text. For example, if 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 'sysrp.txt 'file contains this string. The sysrq function is discussed.

By default, 'grep' only searches for the current directory. If the directory contains many subdirectories, 'grep' is listed as follows:
Grep: Sound: is a directory
This may make the output of 'grep' difficult to read. There are two solutions:
Search for subdirectories: grep-R
Ignore subdirectory: grep-D skip

Of course, if you expect a lot of output, you can use the Pipeline
Switch it to 'less 'to read:
$ Grep magic/usr/src/Linux/documentation/* | less
In this way, you can read more conveniently.
Note that you must provide a file filtering method (* for searching all files *). If you forget, 'grep' will wait until the program is interrupted. If this happens, press Ctrl + C and try again. Important !! Command line parameters:
Grep-I pattern files: searches case-insensitive. Case Sensitive by default,
Grep-l pattern files: only names of matched files are listed,
Grep-l pattern files: Lists unmatched file names,
Grep-W pattern files: match only the entire word, not a part of the string (for example, match 'Magic ', not 'magical '),
Grep-C number pattern files: the matching context displays the rows of [number,
Grep pattern1 | pattern2 files: displays the rows matching pattern1 or pattern2,
Grep pattern1 files | grep pattern2: displays rows that match both pattern1 and pattern2.

Here are some special symbols used for search:
\ <And \> respectively indicate the start and end of a word.
For example:
Grep man * matches 'Batman ', 'manic', 'Man ', etc,
Grep '\ <man' * matches 'manic 'and 'man', but not 'Batman ',
Grep '\ <man \>' only matches 'man ', not other strings such as 'Batman' or 'manic.

'^': Indicates the first row of the matched string,
'$': Indicates the end of a matched string,
If you are not familiar with command line parameters, you can try 'grep' on the GUI, such as rexgrep. The software provides and, or, not, and other syntaxes, as well as beautiful buttons :-). If you only need more explicit output, try fungrep.

 

Common grep command syntax 1. Double quotation marks and single quotation marks
When you input a string parameter in the g r e p command, it is best to enclose it with double quotation marks. For example, "m y s t r I n g ". There are two reasons for doing this: one is to prevent being misunderstood as the s h e l command, and the other is to find strings composed of multiple words, such as "jet plane ", if double quotation marks are not used, the word p l a n e will be mistaken for a file, and the query result will return the error message "the file does not exist.
When you call a variable, double quotation marks should also be used, such as: g r e p "$ m y va R" file name. If not
No results are returned.
When matching the call mode, use single quotes. [[email protected] # echo 'grep 123 111.txt '(# note that it is a single quotation mark)

2. Common g r e p options include:
-C only outputs the Count of matched rows.
-I is case insensitive (only applicable to single characters ).
-H: When querying multiple files, the file name is not displayed.
-L only names containing matching characters are output when multiple files are queried.
-N: the matching row and row number are displayed.
-S does not display the error message that does not exist or does not match the text.
-V: displays all rows that do not contain matched text.

3. Special-query multiple files
$ Grep "sort" *. DOC (# search for the string "s o r t" in all. d o C files in the current directory ")
 
$ Grep "sort it" * (# or query the word "sort it" in all files ")
All the following examples are used to query a single file.
4. Row matching
$ Grep-C "48" data. f
$4 (# g r e p returns the number 4, meaning four rows contain the string "4 8 ″.)
$ Grep "48" data. F (# display four lines of text containing the "4 8" string)

5. display the number of rows that meet the matching mode:
[[Email protected] oid2000] # grep-N 1234 111.txt
:1234
3: 1234ab

6. Exact match
[[Email protected] oid2000] # grep "1234 \>" 111.txt
1234

7. query empty rows. query the rows that start or end with a condition.
Use ^ and $ to query empty rows. Use the-n parameter to display the actual number of rows
[[Email protected] oid2000] # grep-n "^ $" 111.txt (return result 2: # the second line is empty)
[[Email protected] PC oid2000] # grep-n "^ ABC" 111.txt (# query rows starting with ABC)
[[Email protected] oid2000] # grep-N "ABC $" 111.txt (# query rows ending with ABC)

8. Match special characters and query characters with special meanings, such as $. '"* [] ^ | \ +? , Must be preceded by a specific character \.
[[Email protected] oid2000] # grep "\." 111.txt (#query all rows containing "." In 111.txt)
[[Email protected] oid2000] # grep "My \. conf" 111.txt (# query rows with the file name my. c o n f)

9. directory query
[[Email protected] oid2000] # ls-L | grep "^ d" (# If you want to query the directories in the Directory List)
[[Email protected] oid2000] # ls-L | grep "^ d [d]" (# query all files that do not contain a directory in a directory)
[[Email protected] # ls-L | GRPE "^ d ..... X. x "(# query the directory set with executable permissions of other users and user group members)

Detailed explanation of grep COMMANDS IN SHELL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.