grep command in Linux

Source: Internet
Author: User
Tags character classes

grep is a powerful text search tool that uses regular expressions to search for text and print matching lines.

grep common usage
' Search String ' The filename option and parameter:-A: The number of times the binary file is searched for the data ' search string ' as atext file -  I: Ignoring the difference in case, so the case is considered the same -N: The line thatoutputs the row number ' search string '  content! --color=auto: Can be found in the keyword part of the color display Oh!

1. The output of the row containing W in filename

[[email protected] log]# grep w filename wo ai wo jiawo hen haoda jia Hao wo shi yi ge hao ren

2. Show the line number

[[email protected] log]# grep-1: Wo ai wo jia3: Wo hen hao4:d a jia H Ao wo shi yi ge hao ren

3. How many lines contain a matching pattern

[[email protected] log]# grep-3

4. Combined with regular expressions

Search for character classes : Search for the two words of test or taste, [] There are several bytes in it, he would like to represent a "one" byte

is is Huan 

Reverse Search --inverse selection of character classes [^]: If you want to search for a line that has OO, but do not want to have g in front of Oo, the following

' [^g]oo '  8 is a super

beginning of line with trailing bytes ^ $

Attention:

^ symbols, which are different from outside of the character class symbols (brackets [])! In [] represents the "reverse selection", outside [] represents the meaning of positioning at the beginning of the line!

As long as the line at the beginning is Wo

[[email protected] log]# grep-n ^1: Wo ai wo jia3: Wo hen hao

find a blank line ----Only the beginning and end of the line (^$)

' ^$ '  6:9:ten:

Any one of the bytes. With repeating bytes *
The meanings of these two symbols in regular expressions are as follows:

. (decimal point): means "must have an arbitrary byte" meaning; * (asterisk): represents "repeating the previous character, 0 to infinity" meaning, for the combined form

[Email protected] log]# grep-n ' G.. d ' filename
8:good is a gread

grep command in Linux

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.