Deep understanding of the grep command in Linux _linux shell

Source: Internet
Author: User
Tags character classes regular expression

Introduced

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.

 
 

Parameters:

-A: Find data in text file as binary file

-C: Calculates the number of find ' Find string '

-I: Ignore case difference, that is, the case is treated as the same

-N: By the way output line number

-V: Reverse selection, which shows the line with no ' find string ' content

-L: Only matching files are displayed, no specific content is displayed

-f< Rule File >: Specifies a rule file that contains one or more rule styles, allowing grep to find the contents of the file that match the rule criteria in a rule style per line.

Instance:

#取出文件 the line containing the Manpath keyword in the/etc/man.config, and add the Found keyword color
grep--color=auto manpath/etc/man.config

#把ls- The output of L contains the letter file (case-insensitive) content output
ls-l | grep-i file

#在文件 '/var/log/messages ' to find the keyword "Aug" 
grep aug/var/log/ Messages 

#在文件 '/var/log/messages ' to find row grep starting with "Aug"
^aug/var/log/messages 

#选择 '/var/log/messages ' All rows in the file containing numbers 
grep [0-9]/var/log/messages 

#在目录 '/var/log ' and a subsequent directory search string "Aug" (Recursive lookup)
grep aug-r/var/log/* Files 

#在当前目录搜索带 ' energywise ' rows
grep ' energywise ' *      

#在当前目录及其子目录下搜索 ' energywise ' file
grep-r ' EnergyWise ' *  

#查找指定进程
ps-ef|grep svn

#从文件中读取关键词进行搜索, output test.txt file contains the contents
of the keyword read from the test2.txt file. Cat Test.txt | Grep-f Test2.txt

grep and Regular Expressions:

Character class Search: If I want to search for test or taste these two words, I can find that they actually have the common ' t?st ' existence ~ this time, I can search for:

Grep-n ' t[ae]st ' regular_express.txt

In fact [] there are a few bytes inside, he would like to represent a "one" byte, so, the above example shows that I need the string is "tast" or "test" two strings!

Reverse selection of character classes [^]: If you want to search for OO rows, but don't want oo preceded by G, the following

Grep-n ' [^g]oo ' Regular_express.txt

Continuation of character class: Again, suppose I don't want to have a little writing section in front of Oo,

Grep-n ' [^a-z]oo ' Regular_express.txt

When we are in a set of collection bytes, if the byte group is contiguous, such as uppercase/lowercase English/numerals, etc., you can use [a-z],[a-z],[0-9] to write, so if our request string is number and English? Oh! Write him all together and become: [a-za-z0-9].

We're going to get the number line, and that's it:

Grep-n ' [0-9] ' regular_express.txt

Beginning and end of line byte ^ $

Line start character: What if I want to make the only list at the beginning of the line? This time you have to use the location byte! We can do this:

Grep-n ' ^the ' regular_express.txt

The line that starts with the little writing section is listed? You can do this:

Grep-n ' ^[a-z] ' regular_express.txt

I don't want to start with an English letter, then it can be this:

Grep-n ' ^[^a-za-z] ' regular_express.txt

^ symbol, within the character class symbol (parentheses []) is different from outside! Represents "Reverse selection" within [], and outside [] represents the meaning of positioning at the beginning of the line!

Line ending with a decimal point (.) At the end of the row:

Grep-n ' \.$ ' regular_express.txt

Note in particular that because the decimal point has other meanings (described below), you must use the escape character () to remove its special meaning!

#找出空白行
grep-n ' ^$ ' regular_express.txt

Any one byte. With Duplicate bytes *

. (decimal point): Represents "must have an arbitrary byte" meaning;

* (asterisk): "Repeat the previous character, 0 to infinity" means, for the combined form

#需要找出 g?? D's string, which is a total of four bytes, starts with G and ends with D, and I can do this:
grep-n ' G. d ' regular_express.txt

#查找至少两个 o above the string, "o*" is represented by: "Possession of empty bytes or an O-above byte"
grep-n ' ooo* ' regular_express.txt

# You want the string to begin and end with G, but only two g can exist between at least one O, i.e. Gog, Goog, Gooog .... Wait
grep-n ' goo*g ' regular_express.txt

#找出 G to the beginning of the line with G, in which the characters are dispensable
grep-n ' g.*g ' regular_express.txt

# Find a string of two o, which can be:
grep-n ' o\{2\} ' regular_express.txt

#找出 G followed by 2 to 5 O, then another G string, he would be like this:
grep-n ' go\{2 , 5\}g ' Regular_express.txt

#2 an o above goooo....g? In addition to being goo*g, you can also
grep-n ' go\{2,\}g ' regular_express.txt

#从多个文件中查找关键词

Summarize

The above is about the content of the grep command in Linux, I hope the content of this article for everyone's study or work can bring some help, if you have questions you can message exchange.

Related Article

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.