grep command tips in Linux systems

Source: Internet
Author: User
Tags glob regular expression

Linux grep is a command that matches and searches on text and content based on rows.


The grep command syntax and common option are as follows:
Usage: grep [options] ... pattern [FILE] ...
Find pattern in each FILE or standard input.
The default pattern is a basic regular expression (abbreviated as BRE).
For example: Grep-i ' Hello World ' menu.h main.c

--color: Coloring a matching string to print
-O: Displays only what is matched to the pattern
-C: Print out the number of rows that match to the string
-i:ignore case, does not distinguish between character case
-V: Shows rows that cannot be matched by a pattern
-E: Using extended regular expressions
-N: Prints the line number in the file that matches the string
-R,-r: recursively each file in all folders
-F: Get pattern from FILE
Stop-m:num after a second match
-L: Print out file names that contain matching strings, not matching content
-L: Prints a file name that does not contain a matching string, as opposed to the-l option
-H: Print out the file name of the string for each matching string
-H: Do not print filename of file that matches string
-W: Force pattern to match only words exactly
-X: Force pattern to match only one row exactly
-A N: Print matching strings while printing the first N rows of all rows matching the string
-B N: Print matching strings while printing the following N lines of the line that matches the string
-C N: Print matching string at the same time print matching string of rows of the first n rows and after n lines
--exclude=glob: Skip GLOB matching filename for search
--exclude-dir=dir: Skips the folder specified by Dir in recursive search

grep common usage:
(1) Recursively search for files in the current directory and display only file names
grep "Redis". -r-l

(2) Search for the specified string and display the line number
cat/etc/passwd | Grep-in "Mysql"

(3) Read the keyword from the specified file to search
root@mckee-pc:~# Cat Test.txt
Linux
Peida.cnblogs.com
Ubuntu
Ubuntu Linux
Redhat
Redhat
LinuxMint
root@mckee-pc:~# Cat Test2.txt
Linux
Redhat
root@mckee-pc:~# Cat Test.txt | Grep-f Test2.txt
Linux
Ubuntu Linux
Redhat
LinuxMint

(4) matches a row at the end of "at"
Cat Test.txt | Grep-e "at$"

(5) Matches only words and displays the number of matching rows
Grep-w-C "usr"/etc/passw

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.