grep, Egrep, and Fgrep of the Linux Text Processing tool

Source: Internet
Author: User
Tags egrep

first, the basic concept

expression: A regular expression is a logical formula for string manipulation, which is a "rule string" that is used to express a filter logic for a string, using predefined specific characters and combinations of these specific characters.

grep(Global search Regular expression (RE) and print out of the line): A text Search tool that searches the target file based on a user-specified text pattern, Displays the rows that can be matched to the pattern (as a text filter)

Egrep: use extended regular expressions to build patterns, i.e. GREP-E

fgrep: fast, does not parse the regular expression, which is grep–f

Meta-character: does not represent the meaning of the character itself, for the description of the additional functionality

Ii. how grep is used

Format:

grep [OPTIONS] PATTERN [FILE ...]

eg.# grep "Root"/etc/passwd

Common options:

-E: Using extended regular expressions to build patterns

-F: Using non-parsing regular expressions to build patterns

-I: Character case insensitive, ignore-case

-V: Reverse, showing rows that cannot be matched by the pattern

-O: Displays only strings that are matched to the pattern, not the entire row

-A #: match to the line and the # line below it

-B #: Match to the line and the # line above it

-C #: Match to the line and the # line next to it

--color: Specifies the color that is matched by the pattern to the character, the parameter options are never,always and auto

The standard is metacharacters the expression:
1, character matching:

. : Any single character

[[email protected] ~]# grep--color=auto "RO.T"/etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/ Root:/sbin/nologin
[]: Any single character within the specified range

[0-9], [[:d Igit:]]: Numeric characters
[A-z], [[: Lower:]]: lowercase characters
[A-z], [[: Upper:]]: Uppercase characters
[[: Alpha:]]: uppercase and lowercase characters
[[: Alnum:]]: Numbers and uppercase and lowercase characters
[[: Space:]]: white space characters
[[:p UNCT:]]: punctuation characters

650) this.width=650; "Style=" background-image:none;padding-left:0px;padding-right:0px;border-top-width:0px; border-bottom-width:0px;border-left-width:0px;padding-top:0px; "title=" image "alt=" image "src="/HTTP/ Img1.51cto.com/attachment/201409/6/9185783_1409986312ilkq.png "border=" 0 "height=" 262 "width=" 677 "/>

[^]: Any single character outside the specified range

650) this.width=650; "Style=" background-image:none;padding-left:0px;padding-right:0px;border-top-width:0px; border-bottom-width:0px;border-left-width:0px;padding-top:0px; "title=" image "alt=" image "src="/HTTP/ Img1.51cto.com/attachment/201409/6/9185783_1409986313n9qs.png "border=" 0 "height=" 347 "width=" 788 "/>

2, number of times match: used to specify the number of times to match the preceding characters

*: The preceding character any time

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;padding-left:0px; padding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "alt=" image "src="/HTTP/ Img1.51cto.com/attachment/201409/6/9185783_1409986376ixkq.png "border=" 0 "height=" 203 "/>

NOTE: * Any time before the character

. *: matches any character of any length

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;padding-left:0px; padding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "alt=" image "src="/HTTP/ Img1.51cto.com/attachment/201409/6/9185783_1409986376cblu.png "border=" 0 "height=" 331 "/>

\? : The preceding character 0 or 1 times

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;padding-left:0px; padding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "alt=" image "src="/HTTP/ Img1.51cto.com/attachment/201409/6/9185783_1409986376k6yq.png "border=" 0 "height=" 151 "/>

Note: The grep pattern uses greedy mode, that is, to match characters as long as possible;

\{m\}: Matches M-Times

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

\{m,\}: at least m times;

\{0,n\}: Up to n times;

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;padding-left:0px; padding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "alt=" image "src="/HTTP/ Img1.51cto.com/attachment/201409/6/9185783_1409986376gnwt.png "border=" 0 "height=" 295 "/>

3. Position anchoring: used to specify where characters appear

^: Anchoring the beginning of the line

eg. ^char

$: Anchor Line end

eg. char$

^$: Blank line

\<char: Anchor word head, \bchar

char\>: Anchor ending, char\b

4. Group:

\(\)

eg. \ (ab\) *xy

Reference:

\1: A forward reference that references the first opening parenthesis and the corresponding pattern in the closing parenthesis.

\ 2: a back reference, referring to the previous second opening parenthesis, and to the contents of the pattern in the corresponding closing parenthesis

...

eg. \ (a.b\) xy\1:a6bxya6b

650) this.width=650; "Style=" background-image:none;border-bottom:0px;border-left:0px;padding-left:0px; padding-right:0px;border-top:0px;border-right:0px;padding-top:0px; "title=" image "alt=" image "src="/HTTP/ Img1.51cto.com/attachment/201409/6/9185783_1409986376fumi.png "border=" 0 "height=" 148 "/>

Note: match the exact match of the reference, and the string is exactly the same

The standard is metacharacters the expression:

Character Matching:

. : Any single character

[]: Any single character within the specified range

[^]: Any single character outside the specified range

Number of matches:

*: Match the characters in front of it any time;

? : matches the preceding character 0 or 1 times;

+: match the characters in front of it at least 1 times

{m}: Matches the preceding character m times;

{m,n}: Matches the preceding character at least m times, up to N times

{m,}: matches the characters preceding it at least m times;

{0,n}: Matches the preceding character up to n times;

Anchoring:

^: Beginning of the line

$: End of line

\<, \b: The head of theword

\>, \b: suffix

Group:

(): Group

| : Or, AC|BC, means AC or BC, or the entire string

eg. Grep-e "Con (c|c) at"

Conc or Cat

Concat or Concat

Iii. Intensive Exercises

Lab Environment:

Hardware: Thinkpad T430 i5-3320m 4GB RAM 500GB HDD

Virtual machine: 10.0.2 build-1744117

Operating system: CentOS 6.5

1. Display the lines in the/proc/meminfo file that begin with the case s;

# grep "^[ss]"/proc/meminfo

2, remove the default shell for non-bash users;

# grep-v "bash$"/etc/passwd | Cut-d:-f1

3. Remove the user whose default shell is bash and its ID number is the largest;

# grep "bash$"/etc/passwd | Sort-n-T:-k3 | Tail-1 | Cut-d:-f1

4, display/etc/rc.d/rc.sysinit file, start with #, followed by at least one white space character, and then have at least one non-whitespace character line;

# grep "^#[[:space:]]\{1,\}[^[:space:]]\{1,\}"/etc/rc.d/rc.sysinit

5. Display the line beginning with at least one blank character in/boot/grub/grub.conf;

# grep "^[[:space:]]\{1,\}[^[:space:]]\{1,\}"/boot/grub/grub.conf

6, find out the/etc/passwd file in one or two digits;

# grep--color=auto "\<[0-9]\{1,2\}\>"/etc/passwd

7. Find the integer between 1 and 255 in the result of ifconfig command;

# Ifconfig | Grep-e--color=auto "\< ([1-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]) \> "

8. View all the information of the root user on the current system;

# grep "^root\>"/etc/passwd

9, add user bash and Testbash, basher, and then find the current system on its user name and the default shell of the same user;

# grep--color=auto "^\ ([[: Alnum:]]\{1,\}\) \>.*\1$"/etc/passwd

10. Find the line ending with "LISTEN" or "established" in the result of netstat-tan command execution;

# Netstat-tan | Grep-e--color=auto "(listen| Established) [[: space:]]*$]

11, remove all the users of the current system shell, requirements: Each shell is displayed only once, and in ascending order;

# cut-d:-f7/etc/passwd | Sort-u

12. Write a script that counts the number of rows in the/etc/rc.d/rc.sysinit,/etc/init.d/functions, and/etc/fstab files that begin with # and the number of rows in a blank row;

#!/bin/bashfor file In/etc/rc.d/rc.sysinit/etc/init.d/functions/etc/fstab;do bashline= ' grep-e--color=auto "^#" $fi Le | Wc-l ' spaceline= ' grep-e "^#$" $file | Wc-l ' echo ' $file has $bashline, $file have $spaceline "done

13, write a script, respectively copy/etc/rc.d/rc.sysinit,/etc/init.d/functions and/etc/fstab files to the/tmp directory, the file name is formerly known as the current date composition, (for example, the first file after the name of the copy/ tmp/rc.sysinit-2-14-02-16;)

#!/bin/bashfor path In/etc/rc.d/rc.sysinit/etc/init.d/functions/etc/fstab;do filename= ' basename $path ' CP $path/tmp/$fileName ' date +-%y-%m-%d ' done

14. Write a script

Displays the user name, UID, and line number of all the default shell-bash users on the current system in the/etc/passwd file;

#!/bin/bashcat-n/etc/passwd | grep--color=auto "bash$" | Cut-d:-f1,3

Challenge: Write a pattern that matches the IP address in the real sense; (1.0.0.1--223.255.255.254)

#grep-E--color=auto "\< ([1-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-2][0-3]) \> (.\< ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-5][0-5] \>) {2}.\< ([1-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-5][0-4]) \> "

Extension: Write a script to determine whether the IP address entered by the user is legitimate

#!/bin/bashread-p "Enter a IP Address:" Ipaddressif [[-N ' echo $ipAddress | Grep-e "\< ([1-9]|[ 1-9][0-9]|1[0-9]{2}|2[0-2][0-3]) \> (.\< ([0-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-5][0-5] \>) {2}.\< ([1-9]|[ 1-9][0-9]|1[0-9][0-9]|2[0-5][0-4]) \> "']];thenecho" This is a valid IP address! " Elseecho "This is a invalid IP address!" Fi


This article is from the "Wind Qing" blog, please be sure to keep this source http://fengqingyang.blog.51cto.com/9185783/1549640

grep, Egrep, and Fgrep of the Linux Text Processing tool

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.