Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Grep and regularization

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Grep is a line editor whose default action is to search for rows that match a given pattern to be displayed on the screen. Its variants include egrep and fgrep.

Regular expressions are user-defined pattern templates that the Linux tool uses to filter text. Regular expressions use metacharacters to represent one or more characters in a data stream.

Regular expressions (Regual Expression,REGEXP) include BRE and ERE. BRE is the basic regular expression and ERE is the extended regular expression.

The metacharacters used by grep are metacharacters used by BRE,egrep are ERE, while fgrep does not use metacharacters. These three commands can communicate with each other using different options. Next, let's look at the use of grep.

1. Grep

1. Format

Grep [options] PATTERN [FILE...]

two。 Option

-o: only the parts of the matching line that match PATTERN are displayed.

-I: ignore case differences between PATTERN and input files

-v: inverted to show only lines that are not matched by PATTERN

-Q: silent mode, if a match is found, the execution status of the command returns a value of 0, otherwise it is non-0.

-A NUM: prints the following NUM line immediately after the matching line.

-B NUM: prints the above NUM line immediately before the matching line.

-C NUM: prints each NUM line in the context of the matching line.

2. Basic regular expressions

Types

Metacharacter

explain

Character matching

.

Match any single character

[]

Matches any single character within the specified range

[^]

Matches a single character outside the specified range

Matching times

*

Match the characters before them any time, 0 times, 1 or more times

. *

Match any character of any length

\?

Match the character before it 0 or 1 times, that is, the character in front of it can be meta.

\ +

Match the character before it one or more times, that is, the character before it appears at least once

\ {m\}

Match the character in front of it m times, exactly match

\ {mdirection n\}

Match the characters before them at least m times and n times at most

\ {0magnetic n\}

Match the characters before them up to n times

\ {m,\}

Match the characters before them at least m times

Position anchoring

^

Anchor the beginning of the line, written on the far left side of the pattern, and the specified string can only appear at the beginning of the line

$

Anchor the end of the line, written on the far right of the pattern

^ PATTERN$

Use PATTERN to match the whole line

^ $

Represents a blank line, cannot even contain spaces, and does not include any characters

^ [[: spaces:]] *

A line that represents a blank line or includes a blank character

\

Anchor suffix

\\

Precisely anchor words

Grouping and citation

\ (PATTERN\)

Bind one or more characters together and treat them as a whole

The content matched by the pattern in group parentheses is automatically recorded by the regular expression engine in the internal variables, which are:

\ 1: the character between the first left parenthesis and the matching right parenthesis of the pattern from the left

\ 2: the character between the pattern from the left, the second left parenthesis and the matching right parenthesis

Third, extend regular expressions

Types

Metacharacter

explain

Character matching

.

Match any single character

[]

Matches any single character within the specified range

[^]

Matches a single character outside the specified range

Matching times

*

Match the characters before them any time, 0 times, 1 or more times

. *

Match any character of any length

?

Match the character before it 0 or 1 times, that is, the character in front of it can be meta.

+

Match the character before it one or more times, that is, the character before it appears at least once

{m}

Match the character in front of it m times, exactly match

{m,n}

Match the characters before them at least m times and n times at most

{0,n}

Match the characters before them up to n times

{m,}

Match the characters before them at least m times

Position anchoring

^

Anchor the beginning of the line, written on the far left side of the pattern, and the specified string can only appear at the beginning of the line

$

Anchor the end of the line, written on the far right of the pattern

^ PATTERN$

Use PATTERN to match the whole line

^ $

Represents a blank line, cannot even contain spaces, and does not include any characters

^ [[: spaces:]] *

A line that represents a blank line or includes a blank character

\

Anchor suffix

\\

Precisely anchor words

Or

| |

Or, separated by |, indicating either | left or right |

C | cat means C or cat

Grouping and citation

(PATTERN)

Bind one or more characters together and treat them as a whole

The content matched by the pattern in group parentheses is automatically recorded by the regular expression engine in the internal variables, which are:

\ 1: the character between the first left parenthesis and the matching right parenthesis of the pattern from the left

\ 2: the character between the pattern from the left, the second left parenthesis and the matching right parenthesis

Fourth, the difference between BRE and ERE: from the above two tables, we can see the difference between basic regular expressions and extended regular expressions, that is, the metacharacters of extended regular expressions no longer need to be escaped when matching and grouping times; in addition, extended regular expressions have one more metacharacter or metacharacter.

Exercise: so much has been mentioned above, let's practice it together.

1. Display lines in the / proc/meminfo file that begin with uppercase or lowercase S.

# grep-I'^ s'/ proc/meminfo

# grep'^ [Ss]'/ proc/meminfo

# grep-E'^ (S | s)'/ proc/meminfo

2. Show users whose default shell is not / sbin/nologin in the / etc/passwd file

# grep-v "/ sbin/nologin$" / etc/passwd | cut-d:-f

3. Display the users whose default shell is / bin/bash in the / etc/passwd file; further: show only the users with the largest ID number in the above results

# grep "/ bin/bash$" / etc/passwd | sort-t:-K3-n | tail-1 | cut-d:-F1 Lai7

4. Find one or two digits in the / etc/passwd file

# grep "\" / etc/passwd

# grep "\" / etc/passwd

5. Display lines in / boot/grub/grub.conf that begin with at least one white space character

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

6. Display a line in the / etc/rc.d/rc.sysinit file that begins with #, followed by at least one white space character, followed by at least one non-white space character

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

7. Find the line ending with 'LISTEN' in the execution result of the netstat-tan command

# netstat-tan | grep "LISTEN [[: space:]] * $"

8. Add users bash, testbash, basher, nologin (SHELL is / sbin/nologin), and find users on the current system whose user name is the same as the default shell

# grep "^\ ([[: alnum:]]\ {1,\}\):. *\ 1 $" / etc/passwd

9. Expansion question: create a new text file, assuming the following:

He like his lover.

He love his lover.

He like his liker.

He love his liker.

Find out that the last word is a line made up of a previous word plus r.

[root@liuqing tmp] # grep "\ (l.e\). *\ 1" love.txt

10. Displays the IP address in the output of the ifconfig command.

~] # ifconfig | grep-E-o "\ (.\) {3}"

VI. Exchange of three orders

Grep-E means to use extended regular expressions (egrep), and-F means to treat the pattern as a fixed string (fgrep)

Egrep-G means to use a basic regular expression (grep), and-F means to treat a pattern as a fixed string (fgrep)

Fgrep-E means to use extended regular expressions (egrep) and-G means to use basic regular expressions (grep)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report