Linux Text processing the Three Musketeers grep egrep

Source: Internet
Author: User
Tags egrep

Linux Text processing the Three Musketeers grep egrep

Grep:global search REgular expression andprint out of the line.

function: Text Search tool, according to user-specified "mode (pattern) "Go through the line to search for the target text and print the matching lines."

pattern: An metacharacters character written by a regular expression and a literal character, where the regular expression is a basic regular expression, and an extended regular regular expression class 2

Metacharacters does not indicate its literal meaning, but is used to indicate a wildcard or control function filter conditions. Metacharacters has some of the following

Character Matching:

.: matches any single character

[]: matches any single character within the specified range

[^]: matches any single character outside the specified range

[: Alnum:] represents 0-9 A- Z

[: Alpha:] Representative A-Z

[:d Igit:] Rep 0-9

[: Lower:] represents A-Z

[: Upper:] Representative A-Z

[: Space:] any characters that will produce whitespace, including the space bar [TAB] CR , etc.

Number of matches: (with the basic regular)

*: any time

\? :0 times to 1 times

\+:1 times to several times

\{m,n\}: at least m times, up to n times

\{m\}: exact match m Times

\{0,n\}: up to n times

\{m,\}: at least m times

. *: Any character of any length

Location anchoring:

^: anchor at the beginning of the line, used on the leftmost side of the pattern

$: End of line anchor, used at the far right of the pattern

\<, \b: The first anchor of theWord

\>, \b: Final anchoring

^$: Blank line

\ (\): grouping, the pattern in parentheses matches to the content, and remains in the intrinsic variable,

\1: from the left, the first opening parenthesis, and the pattern that matches the middle of the right parenthesis to match the content

\2: from the left, the 2 opening parenthesis, and the pattern that matches the middle of the right parenthesis paired to the content

The extension regular has the following several, with support Egrep

.: any single character

. *: matches the previous character 0 times to Infinity

[]: Single character in range

[^] : A single character outside the range

+: 1 times to several times

? :0 to 1 times

{m}:

{M,n}

|: equivalent to or (or) mode, such as Gd|good, which means gd and good

(): Find the group string

(): grouping, the pattern in parentheses matches to the content, and remains in the intrinsic variable,

\1: from the left, the first opening parenthesis, and the pattern that matches the middle of the right parenthesis to match the content

\2: from the left, the 2 opening parenthesis, and the pattern that matches the middle of the right parenthesis paired to the content

()+: Judgment of multiple groups

I suggest you use alias to write grep egrep as an alias. This file can be written to ~/.BASHRC, which takes effect immediately when the source ~/.BASHRC is executed . This will have a color display.

Alias grep= ' grep--color=auto '

Alias egrep= ' Egrep--color=auto '

use format:grep [OPTIONS] PATTERN [FILE ...]

options are --color=auto

-V: display mode does not match rows

-I: Ignore case

-o: Show only rows with pattern matching

-Q: silent mode

-E: using extended regular

-N: Show line Numbers

Homework Exercises:

3: Display /etc/passwd in the file to Bash End of Line

grep ' bash$ '/etc/passwd

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/72/8E/wKiom1XmgROwFpMQAAFbMcePCYA870.jpg "title=" 1.png " alt= "Wkiom1xmgrowfpmqaafbmcepcya870.jpg"/>



4: Display /etc/passwd two-digit or three-digit number in a file

egrep ' \b[0-9]{2,3}\b '/etc/passwd

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/72/8E/wKiom1XmgWySztO8AAREQ8nDliQ316.jpg "title=" 2.png " alt= "Wkiom1xmgwyszto8aareq8ndliq316.jpg"/>


5: Display the ' Netstat-tan ' command results with ' LISTEN ' followed by 0, one or more whitespace characters end of the line

netstat-ant | egrep ' listen[[:space:]]*$ '

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/72/8B/wKioL1Xmg_ez_9VWAAD3rH5CtLU591.jpg "title=" 3.png " alt= "Wkiol1xmg_ez_9vwaad3rh5ctlu591.jpg"/>


6:Add UserBash,Testbash,basheras wellNologinUsers (NologinUser'sShellto be/sbin/nologin); then find out/etc/passwdthe user name in the file and itsShellrows with the same name

egrep ' ^ (\b[[:alnum:]]+\b). *\1$ '/etc/passwd

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/72/8B/wKioL1XmhUfhL_i2AAE5HZT5ses539.jpg "title=" 4.png " alt= "Wkiol1xmhufhl_i2aae5hzt5ses539.jpg"/>


7: Display on current systemRoot,CentOSorUser1User's defaultShelland theUID(Please create these users beforehand, if not present)

egrep ' ^root|^centos|^user1 '/etc/passwd | cut-d:-f3,7

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/72/8B/wKioL1XmhbjzJjllAAA0oQMaawU748.jpg "title=" 5.png " alt= "Wkiol1xmhbjzjjllaaa0oqmaawu748.jpg"/>


8: Find out /etc/rc.d/init.d/functions a line with a set of parentheses followed by a word in the file (underlined in the middle of the word)   

grep ' \b[[:alpha:]_]\+\b () '/etc/rc.d/init.d/functions

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/72/8E/wKiom1Xmg_PyvbqXAAD9u5gnxMg693.jpg "title=" 6.png " alt= "Wkiom1xmg_pyvbqxaad9u5gnxmg693.jpg"/>


9: Find out ifconfig in the command execution result 1-255 the number between

ifconfig | egrep--color ' \< ([1-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \> '

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/72/8B/wKioL1XmhnHjcC64AAHeM4QmU3I572.jpg "title=" 7.png " alt= "Wkiol1xmhnhjcc64aahem4qmu3i572.jpg"/>


10: Use echo to output a path, and then egrep to find its path base name; Further use Egrep to remove its directory name

The problem can't be done, it's not written.

This article is from the "CENTOS6 Learning" blog, please make sure to keep this source http://wskfnso.blog.51cto.com/4025557/1690763

Linux Text processing the Three Musketeers grep egrep

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.