The old three grep of the Three Musketeers of Linux

Source: Internet
Author: User
Tags repetition

Description

The grep command in a Linux system is a powerful text search tool that uses regular expressions to search for text and print matching lines. we often use it to filter out the data we want in our work.

Format:

grep [OPTIONS]

Basic parameters:           

  -I is case insensitive

-V excludes content, which is reversed

-N Prints the corresponding line number to the matched content

-e Using extended regular expressions (equivalent to egrep)

-R recursively reads the file under the directory (that is, the file is included in the subdirectory)

-C counts the rows to match

-O Show only what matches to

-A (after) matches the output content line and outputs the specified line after the content line

-B (before) matches the output content line and outputs the specified line before the content line

-C (context) matches the output content line and outputs the specified row before and after the content line

--color Filtered content Display color

Recommended configuration aliases:

echo "Alias grep='grep--color'" >>/etc/profile/ config alias        /etc/profile                                       //Reread the file to make the alias effective

Effect:

[[email protected] test] # grep ". *"/etc/passwd Root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin 

Common usage:

①GREP-IVNEOC ... Match content file name

[[email protected] test] # grep-n "Root"/etc/passwd1:root: x:0:0:root:/root:/bin/bash11:o perator:x:11:0:operator:/root:/sbin/nologin

②grep-r "Matching content" file directory/*

[[Email protected] ~] # grep-r "www" test/*        Test  is http//www. cnblogs.com/caiyundo/Test/dudu/3.txt:www. Cnblog.com/caiyun

③grep "Match Content"-A specifies the row file name

[[Email protected] ~] # grep "root:x"-na 5/etc/passwd1:root:x: 0:0:root:/root:/bin/bash2-bin:x:1:1:bin:/bin:/ sbin/nologin3-daemon:x:2:2:daemon:/sbin:/sbin/nologin4-adm:x:3:4:adm:/var/adm:/sbin/  Nologin5-lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin6-sync:x:5:0:sync:/sbin:/bin/sync

④ Command | grep "Match content"

[[Email protected] ~] # ps-ef |grep "sshd" Root        1261      1  0 Dec12?        00:00:00/usr/sbin/sshd   

Extended:

Regular expressions are a set of rules and methods that are defined for processing a large number of strings, and Linux regular expressions are commonly used in the Three Musketeers of Linux.

Test scenario Simulation (direct copy and paste):

Cat  >>text.txt<<EOF       is  Caiyun.       I like badminton, snooker, running             maybe I'm not a smart person, but I'm workin G hard.         is http://www.cnblogs.com/Caiyundo/       Welcome to my blog! Caiyun              is 791111890       is [email protected]       EOF
View Code

First, Basic Regular Expressions

^ The expression of ... Character Start, ^word

[[email protected] test] # grep-n "^my" Text.txt1:My is Caiyun. 5:My is HTTP://WWW.CNBLOGS.COM/CAIYUNDO/8:my is 7911118909:my  is [Email protected]

$ denotes A. End of character, word$

[[email protected] test] #   is caiyun.  4:maybe i'm not a smart person, but I'm working hard.   is [Email protected] .

^$ represents a null character and can be interpreted as a blank line

[[email protected] test] # grep-n "^$" Text.txt   3:7:

. Indicates matching any single character

[[email protected] test] #  2baDminton, snooker, running4:maybe i'but i'm Working hard.  is http://www.cnblblog! Caiyun

\ means escape character

[[email protected] test] # grep-n "\.$" Text.txt  is Caiyun.  4:maybe i'm not a smart person, but I'm working hard.   is [Email protected] .

* Indicates repetition of the first 0 or more than 1 characters

[[email protected] test] #  2bAdminton, snooker, running4:maybe i'but i'm Working hard.  is http://www.cnblblog! Caiyun

. * Means matching all

[[email protected] test] # grep-n ". *" Text.txt     is Caiyun. 2: I like badminton, snooker, running3:4:maybe i'm not a smart person, but I' m working hard.  is HTTP://WWW.CNBLOGS.COM/CAIYUNDO/6: Welcome to my blog! Caiyun7: is is [email protected]

[] indicates any single character matching "[]"

[[email protected] test] # grep-n "[791]" Text.txt    is 791111  is Caiyun111111@gmail. com.    

[^] denotes any single character inside the inverse "[^]"

[[email protected] test] # echo Caiyun >> test2.txt [[email protected] test] # echo 791111890 >> test2.txt        [[email protected] test] # grep-n "[^0-9]" Test2.txt1:caiyun

[-] denotes any single character that matches a paragraph in "[-]", such as [0-9] i.e. 0 to 9

[[email protected] test] # grep-n "[0-9]" Text.txt     is 791111890  is Caiyun111111@gmail. com.

1\{4\} indicates that the match character "1" Repeats 4 times

[[email protected] test] # grep-n "1\{4\}" Text.txt  is1111 caiyun1111[email protected]

1\{5,\} indicates that the match character "1" repeats 5 or more than 5 times

[[email protected] test] #   is Caiyun111111@gmail. com.

1\{,6\} indicates that the match character "1" repeats 6 times and 6 times or less

[[email protected] test] #   is Caiyun. 2: I like badminton, snooker, running3:4:maybe i'm not a smart person, but I' m working hard.  is HTTP://WWW.CNBLOGS.COM/CAIYUNDO/6: Welcome to my blog! Caiyun7: 1111 is Caiyun111111@gmail. com.

1\{3,5\} indicates that the match character "1" repeats 3-5 times

[[email protected] test] # grep-n "1\{3,5\}" Text.txt       is1111 caiyun11111[email protected]

Second, extended regular expression

+ means to repeat all characters preceding "one or more" ("*" is 0)

[[email protected] test] # grep-n "11111*" Text.txt     is1111 is caiyun111111@gmail. com.[ [email protected] test]# is Caiyun111111@gmail. com.

? Represents the repetition of "0 or one" of the preceding characters ("." Yes and only 1)

[[email protected] test] # grep-n "11111." Text.txt    is Caiyun111111@gmail. com. [[email protected] test] #   is1111 caiyun11111[email protected]

| means filtering multiple strings

[[email protected] test] #  6Caiyun is [email protected]mail.com.[ [email protected] test]#  grep-n "Caiyun\|mai" Text.txt  6Caiyun is [ Email protected]mail. com.

In general, when we need to use the extended regular expression match, the grep to add the parameter "-E" or use "\" translation character escape match

Three, meta characters

\b Boundary character, word boundary

[[email protected] test] # grep ' Caiyun ' text.txt     is Caiyun are http://www.cnblogs.com/caiyundo/is   Caiyun[email protected][[email protected] test]#  grep ' caiyun\b ' Text.txtisCaiyun.

grep There are many ways to use it, as long as it conforms to rules and logic, and can be used in conjunction with many other commands.

PS: To ingenious, more use more practice.

The old three grep of the Three Musketeers of 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.