The small theory of LINUX--grep

Source: Internet
Author: User
Tags egrep

Linux--grep This is a very important tool, Sed,awk,grep is called the 3 swordsman of Linux!

grep: Searches the text according to the pattern, and displays the line of text that conforms to the pattern.

To cite an example, grammar:

Example 1: #grep-i ' root '/etc/passwd #找出/etc/passwd the root or root line in this file,-I is ignore , ignoring the meaning, ignoring the case.

------------------------------------------

Example 2: #grep-V ' root '/etc/passwd #找出/etc/passwd This file, the rows that are processed with root are not displayed

Out, the rest of the lines are shown,-V is invert, so invert

------------------------------------------

Example 3: #grep-o ' root '/etc/passwd #找出/etc/passwd This file, matches the word, not the line!

Just show the word root, and you'll know when you knock it all over.

The function of the word. -O, is only, just, only!

-----------------------------------------

Example 4: #grep--color ' root '/etc/passwd #找出/etc/passwd the root row in this file, using

The color is displayed. --color, color.

Tip: #alias grep= ' grep--color ' #有了这个别名以后, directly with grep, without adding--color, will

The color is displayed directly, but the computer restarts, it will fail.

--------------------------------------------------------------------------------

Example 5: #grep-A ' model '/proc/cpuinfo #意思是显示出model及其后面的2行,-A, want after

--------------------------------------------------------------------------------

Example 6: #grep-B ' model '/proc/cpuinfo #意思是显示出model及其前面的2行,-B, want to before

------------------------------------------

A little bit of knowledge:

Regular Expressions: (English: REGular expression----REGEXP)

*: Asterisk is any character of any length

? : The question mark is any single character

[]: The brackets match any single character within the specified range, such as: [0-9] is a number from 0 to 9

Example: #grep--color [0-9]/etc/inittab 0 to 9 of the numbers will be displayed. Knock yourself,

Look at the effect. Only if you knock more, you understand.

--------------------------------------------------------------------------------

Continue to say:

[^]: brackets with a sharp-pointed number, which matches any single character outside the specified range

Example: #grep--color [^0-9]/etc/inittab you look at the effect, in addition to the 0-9 Arabic numerals,

All the other letters are displayed.

---------------------------------------------------------------------------------

Character set: [: Alpha:] #意思是所有字母的集合

[:d igit:] #意思是所有数字的集合

[: Upper:] #意思是所有大写字母的集合

[: Lower:] #意思是所有小写字母的集合

[: Alnum:] #意思是所有字母和数字的集合

[:p UNCT:] #意思是所有标点符号的集合

[: Space:] #意思是所有空格的集合

Example: grep ' [[: Upper:]] '/etc/passwd #意思是列成/etc/passwd all the files in this file

Capital.

------------------------------------------------------------------------

Number of matches:

*: The asterisk matches any of the characters in front of it, including 0 times, or countless times, at any time.

. *: Dots and asterisks, which represent any character of any length! Note the comparison with an asterisk alone.

-------------------------------------

? : A single question mark that matches the preceding character once or 0 times.

Example: grep ' a\?b '/etc/passwd #意思是把b, or 1 A in front, then 1 b

The words show up.

--------------------------------------------------------------

\{m,n\}: Matches its preceding character at least m times, up to N times.

Example: grep ' a\{1,3\} '/etc/passwd

\{1,\} #最少1次, up to countless times

\{0,3\} #最少0次, up to 3 times, 0 and 3 separated by commas


-------------------------------------

^: a bracket character that indicates that the content after this character must appear at the top of the line.

$: Dollar character that indicates that the content before this character must appear at the end of the line.

Example: # grep ' ^a '/etc/passwd means listing a letter must appear at the beginning of the line

# grep ': $ '/etc/passwd means to list the lines that the colon must appear at the end

Tip: ^$: The combination of this character represents a blank line.

--------------------------------------

\b or \<: means that any character following it must appear as the header of the word.

\b or \>: means that any character in front of it must appear as the tail of the word.

Compare this symbol to ^ and $ to see what's different.

Comparison:

#grep--color ' \<root '/etc/passwd

#grep--color ' ^root '/etc/passwd

Tip: \<root\> This represents the line for these 1 words, root!


---------------------------------------------------


Group:

\( \)

#grep ' \ (root\). * \1 '/etc/passwd #命令中, \1 means to refer again to the contents of the preceding parentheses, root

Comparison: sed ' s#\ (root\) #\1r#g '/etc/passwd A comparison of different, better understanding of sed and grep.


----------------------------------------------------------------



Extensible Regular Expression: (extended regular expression)

What do you mean by extension, for example:

# grep-e ' (root) '/etc/passwd

# grep ' \ (root\) '/etc/passwd

The above 2 commands are to/etc/passwd this file with the root of the line is displayed, but one has a backslash, a no backslash, this is the difference! -e This option plus later, there is no backslash.

#egrep ' (root) '/etc/passwd #注意, egrep = = Grep-e, same function.


The writing is not good, please understand! Can help you a little bit, even if not written in vain.


The small theory of LINUX--grep

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.