Detailed Linux grep command _linux

Source: Internet
Author: User
Tags grep regular expression egrep

This article describes the Linux grep command, as follows:

1. Role

The grep command in a Linux system is a powerful text search tool that uses regular expressions to search for text and print matching rows. The grep full name is global Regular Expression Print, which represents the global regular expression version, and its use permissions are all users.

2. Format

grep [Options]

3. Main parameters

[Options] Main parameters:

-C: Only the count of matching rows is output.
-I: case-insensitive (applies to given only).
-H: Do not display file names when querying multiple files.
-L: Only file names that contain matching characters are output when querying multiple files.
-N: Displays matching rows and line numbers.
-S: Do not display error messages that do not exist or do not match text.
-V: Displays all rows that do not contain matching text.

Pattern Regular Expression main parameter:

\: Ignores the original meaning of special characters in regular expressions.
^: matches the start line of the regular expression.
$: Matches the end line of a regular expression.
\<: Starts with the line that matches the regular expression.
\>: To the end of the line that matches the regular expression.
[]: A single character, such as [a], that is, a meets the requirements.
[-]: range, such as [A-z], that is, a, B, C until Z all meet the requirements.
。 : all the individual characters.
*: There are characters, the length can be 0.

4.grep command using a simple instance

$ grep ' test ' d*

Displays the rows that contain test in all files that start with D.

$ grep ' test ' AA bb cc

Displays the row that matches test in the aa,bb,cc file.

$ grep ' [a-z]\{5\} ' AA

Displays all the lines that contain at least 5 consecutive lowercase characters for each string.

$ grep ' w\ (es\) T.*\1′aa

If West is matched, es is stored in memory and labeled 1, then searches for any character (. *), followed by another ES (\1), and the line is displayed. If you use Egrep or GREP-E, you do not use the "\" to escape, directly written ' W (es) t.*\1′ on it.

5.grep command uses a complex instance

Suppose you are searching for a file with a string ' magic ' in the '/usr/src/linux/doc ' directory:

$ grep magic/usr/src/linux/doc/*
sysrq.txt:* How do I enable the Magic SysRq key?
sysrq.txt:* I Use the Magic sysrq key?

Where the file ' Sysrp.txt ' contains the string, the SYSRQ function is discussed.

By default, ' grep ' searches only the current directory. If there are many subdirectories under this directory, ' grep ' will be listed in the following form:

Grep:sound:Is a Directory

This may make the output of ' grep ' difficult to read. Here are two ways to solve this problem:

Explicitly require searching subdirectories: Grep-r

or ignores subdirectories: grep-d Skip

If you have a lot of output, you can go through the pipe to read on ' less ':

$ grep magic/usr/src/linux/documentation/* | Less

This way, you can read it more easily.

It is important to note that you must provide a file filtering method (*) for all files to be searched. If you forget, ' grep ' will wait until the program is interrupted. If you encounter such a situation, press <ctrl c>, and then try again.

Here are some of the more interesting command line arguments:

    • Grep-i pattern Files: case-insensitive search. The default case is case-sensitive,
    • Grep-l pattern Files: Only the matching file names are listed,
    • Grep-l pattern Files: Lists mismatched file names,
    • Grep-w pattern files: matches only the entire word, not part of the string (such as matching ' magic ', not ' magical '),
    • Grep-c number pattern files: matching contexts show [number] rows,
    • grep pattern1 | PATTERN2 files: Displays rows that match pattern1 or pattern2.
    • grep pattern1 Files | grep pattern2: Displays rows that match both PATTERN1 and pattern2.
    • Grep-n pattern files to display line number information
    • Grep-c pattern files to find total number of rows

Here are some special symbols for searching:

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

For example:

grep man * will match ' Batman ', ' manic ', ' man ' and so on,
grep ' \<man ' * matches ' manic ' and ' man ', but not ' Batman ',
grep ' \<man\> ' matches only ' man ', not ' Batman ' or ' manic ' and other strings.
' ^ ': refers to the matching string at the beginning of the line,
' $ ': refers to a matching string at the end of the line,

Grep Command Usage Encyclopedia

1, Parameters:

-I: Ignore case
-C: Print a matching number of rows
-L: Find include matches from multiple files
-V: Find rows that do not contain a match
-N: Print row and row labels that contain matches

2, RE (regular expression)

\ ignore the original meaning of special characters in regular expressions
^ matches the start line of a regular expression
$ matches the end line of a regular expression
\< starts with the line that matches the regular expression
\> to the end of the line that matches the regular expression
[] A single character, such as [a] that a meets the requirements
[-] range, such as [A-z] that a,b,c until Z meets the requirements
. All the individual characters
* All characters, length can be 0

3. Examples

# Ps-ef | grep in.telnetd Root 19955 181 0 13:43:53? 0:00 in.telnetd # More Size.txt size file content b124230 b034325 a081016 m7187998 m7282064 a022021 a061048 m9324822 03303 a013386 b044525 m8987131 B081016 M45678 B103303 BADc2345 # more Size.txt | grep ' [a-b] ' range, such as [A-z] a,b,c all the time until Z meets the requirements b124230 b034325 a081016 a022021 a061048 b103303 a013386 b044525 # more Siz E.txt | grep ' [a-b] ' * b124230 b034325 a081016 m7187998 m7282064 a022021 a061048 m9324822 b103303 a013386 b044525 m8987 131 B081016 M45678 B103303 BADc2345 # more Size.txt | grep ' B ' single character, as [a] that is a conforming requirement b124230 b034325 b103303 b044525 # more Size.txt | grep ' [BB] ' b124230 b034325 b103303 b044525 B081016 B103303 BADc2345 # grep ' root '/etc/group root::0:root bin: : 2:root,bin,daemon sys::3:root,bin,sys,adm Adm::4:root,adm,daemon uucp::5:root,uucp mail::6:root Tty::7:root,tty, ADM Lp::8:root,lp,adm NUUCP::9:ROOT,NUUCP Daemon::12:root,daemon # grep ' ^root '/etc/group matchStart line of regular expression Root::0:root # grep ' UUCP '/etc/group uucp::5:root,uucp nuucp::9:root,nuucp # grep ' \&LT;UUCP '/etc/group UUCP::5:ROOT,UUCP # grep ' root$ '/etc/group matches the end line of a regular expression Root::0:root mail::6:root # more Size.txt | Grep-i ' B1. *3 '-I: Ignoring case b124230 b103303 B103303 # more Size.txt | Grep-iv ' B1.  
*3 '-V: Find rows that do not contain a match b034325 a081016 m7187998 m7282064 a022021 a061048 m9324822 a013386 b044525 m8987131 B081016 M45678 BADc2345 # more Size.txt | Grep-in ' B1. *3 ' 1:b124230 9:b103303 15:b103303 # grep ' $ '/etc/init.d/nfs.server | Wc-l 128 # grep ' \$ '/etc/init.d/nfs.server |  Wc–l ignores the original meaning of special characters in regular expressions * grep ' \$ '/etc/init.d/nfs.server case "$" in >/tmp/sharetab.$$ ["X$fstype"!= Xnfs ] && echo "$path \t$res\t$fstype\t$opts\t$desc" >>/tmp/sharetab.$$/usr/bin/touch-r/etc/dfs/sharetab/t mp/sharetab.$$/usr/bin/mv-f/tmp/sharetab.$$/etc/dfs/sharetab if [-f/etc/dfs/dfstab] &&/usr/bin/egrep-v ' ^[]* (#|$) ' If [$startnfsd-eq 0-a-f/etc/rmmount.conf] && if [$startnfsd-ne 0]; Then Elif [!-n "$_init_run_level"]; Then while [$wtime-GT 0]; Do wtime= ' expr $wtime-1 ' if [$wtime-eq 0]; Then echo "Usage: $ {Start | Stop} "# more Size.txt the" test file their are files the "" Size.txt "test file their are file S # grep ' \<the ' size.txt the test file their are files # grep ' the\> ' size.txt the test file # grep ' \<

 The\> ' size.txt the test file # grep ' \<[tt]he\> ' size.txt the test file

==================================================================

1, Introduction

A multipurpose Text Search tool that uses regular expressions. This Php?name=%c3%fc%c1%ee "onclick=" Tagshow (event) class= "T_tag" > Command was originally a php?name=% in the Ed row editor C3%fc%c1%ee "onclick=" Tagshow (event) class= "T_tag" > Command/filter:

g/re/p--Global-regular expression-print.

Basic format

grep pattern [File ...]

(1) grep search string [filename]

(2) grep regular expression [filename]

Searches the file for all occurrences of pattern, which can be either a string to search or a regular expression.

Note: When entering strings to be searched, it is best to use double quotes/while using regular expressions in pattern matching, note the use of single quotes

Options for 2,grep

-C outputs only the count of matching rows
-I case-insensitive (for single character)
-n Displays matching line numbers
-V does not show rows that do not contain matching text
-S does not display error messages
-e using extended regular expressions

For more options, see: Man grep

3, commonly used grep instances

(1) Multiple file inquiries

  grep "Sort" *.doc    #见文件名的匹配

(2) Row match: output matching row count

Grep-c "Data.doc"  #输出文档中含有48字符的行数

(3) Display matching rows and rows

 Grep-n "Data.doc"    #显示所有匹配48的行和行号

(4) Show unmatched rows

Grep-vn "Data.doc"   #输出所有不包含48的行

(5) Case sensitive

Grep-i "AB" Data.doc    #输出所有含有ab或Ab的字符串的行

4, the application of regular expressions

(1) Application of regular expressions (note: It is best to enclose regular expressions in single quotes)

grep ' [239]. ' Data.doc   #输出所有含有以2, beginning with 3 or 9, and is a two-digit line

(2) Mismatch test

grep ' ^[^48] ' Data.doc   #不匹配行首是48的行

(3) using extended pattern matching

 Grep-e ' 219|216 ' Data.doc

(4) ... This needs to be applied and summarized in practice, proficient in regular expression.

5, use class name

Class names that can be matched with international patterns:

[[: Upper:]] [A-z]
[[: Lower:]] [A-z]
[[:d Igit:]] [0-9]
[[: Alnum:]] [0-9a-za-z]
[[: Space:]] spaces or tab
[[: Alpha:]] [A-za-z]

(1) using

grep ' 5[[:upper:]][[:upper:]] ' Data.doc   #查询以5开头以两个大写字母结尾的行

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.