Drill-down to Linux grep instructions (practical) _c language

Source: Internet
Author: User
The Linux under grep instructions and find instructions as strong, found the main search file, and grep is content, the two complement each other, take advantage of the weekend, their good summed up a!

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. 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. Escape
^: 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.

3.grep command using a simple instance
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
$ grep ' test ' d*-R <==> grep test d*-R that ' test ' <==> test
Displays the rows that contain test in all files that start with D.
$ grep ' test ' AA BB cc-r
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.
By default, ' grep ' searches only the current directory. If there are many subdirectories under this directory, it is explicitly required to search subdirectories:grep-r

4, for example more size.txt (mainly copy others, standing on the shoulders of master)
# more size.txt

b124230
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
b103303
A013386
b044525
m8987131
B081016
M45678
B103303
BADc2345

# more size.txt | grep ' [a-b] ' //range: If [A-z] that a,b,c until Z meets the requirements, print rows with a-b-ignore case (possibly System dependencies)
b124230
b034325
a081016
a022021
a061048
b103303
A013386
b044525
# more size.txt | grep ' [a-b] ' * //Same result as previous instruction-test results
b124230
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
b103303
A013386
b044525
m8987131
B081016
M45678
B103303
BADc2345

# more size.txt | grep ' B ' //single character; if [a] that is a conforms to the requirement output has b the line
b124230
b034325
b103303
b044525
# more size.txt | grep ' [BB] ' //output with B or B line
b124230
b034325
b103303
b044525
B081016
B103303
BADc2345

# grep ' root '/etc/group //output/etc/group directory with root rows
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 //^: Matches the start line of a regular expression--> the line starting with root
Root::0:root
# grep ' root$ '/etc/group //$: Matches the end line of a regular expression--> the line ending with root
Root::0:root
Mail::6:root

# more size.txt | grep-i ' B1. *3 ' //I: Ignore case
b124230
b103303
B103303

# more size.txt | grep-iv ' B1. *3 ' //-v: Finding rows that do not contain matches
b034325
a081016
m7187998
m7282064
a022021
a061048
m9324822
A013386
b044525
m8987131
B081016
M45678
BADc2345

# more size.txt | grep-in ' B1. *3 ' //-n: Displays matching rows and line numbers.
1:b124230
9:b103303
15:b103303

# more Size.txt
The test file
Their are files
The end

# grep ' the ' size.txt
The test file
Their are files

# grep ' \<the ' size.txt //\< line start
The test file
Their are files

# grep ' the\> ' size.txt //Line End \>
The test file

# grep ' \<the\> ' size.txt //Don't know how to express, hehe.
The test file

# grep ' \<[tt]he\> ' size.txt //And output has the line of the or the
The test file
The end

# grep ' [239]. ' Size.txt //Output all contains rows that start with 2, 3, or 9, and are two digits.

# grep ' ^[^the] ' size.txt //does not match the row at the beginning of the line

# grep-e ' the|test ' size.txt //show rows containing the or test

Note: All of the above have been tested by myself, it can be said that the running on my computer is standard

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.