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

How Linux uses regular expressions

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

Share

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

This article will explain in detail how to use regular expressions in Linux. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

We can use the grep command to test our regular expression, and the grep command uses the regular expression to try to match each line of the specified file and displays all lines that match the expression in at least one place.

The grep command describes:

Common options OPTION:

-c displays the sum of matched rows-o shows only the match itself-v shows non-matching, reverses-I is case-insensitive-E supports extended expression-Q silent mode-n displays the line number of matched lines-A2 displays the last 2 lines of matched lines-B2 displays the first 2 lines of matched lines-C2 displays the first 2 lines of matched lines-V displays version information using syntax: grep [OPTION]... PATTERN [FILE]...

Grep "[match character] match occurrence times" / PATH/TO/SOMEFILE

Character matching:

. Any single character [] within the specified range [^] A single character outside the specified range Reverse. * any character of any length [: alnum:] all letters and numbers [: alpha:] all letters [: blank:] all horizontal white space characters [: cntrl:] all control characters [: digit:] all numbers [: graph:] all printable characters Excluding spaces [: lower:] all lowercase letters [: print:] all printable characters, including spaces [: punct:] all punctuation characters [: space:] all horizontal or vertical white space characters [: upper:] all uppercase letters [: xdigit:] all hexadecimal numbers [= characters =] all characters equal to the specified character

Number of matches:

* the characters in front of them appear any number of times, 0 or 1 times, or multiple times. The character in front of it appears 0 times or 1 time\ + the character in front of it appears 1 or more times, at least 1 time\ {m\} the specified character appears at least m times, at most n times, and the range is taken as n times at most\ m, at least m times.

Position Anchor:

^ Line beginning Anchor, for the leftmost $Line end Anchor of the pattern, and for the rightmost ^ of the pattern. Full line, ^ $blank line, ^ [: space:]] * $blank line or line with blank character\ or\ b suffix anchor\ precise anchor word

Grouping and referencing:

\ {xy\} * ab indicates that multiple characters have 0J1 or more times. \ (abcd\) {1pc3} is used here as an escape character to indicate that the string containing abcd appears at least once. It occurs a maximum of 3 times. * 1\ 1 refers to the reference of the character\ (1.. 2\) between the first () from the left. * (3. 4)\ 2\ 2 refers to the reference of the character between the second () from the left. * (3. 4). * (5. 6)\ 3\ 3 Refers to the character referencing the third () from the left

Examples of use:

View the line that shows the / etc/fstab file that begins with a # comment:

You can also omit [] here:

-v is reversed, that is, lines that are not commented with # are displayed, and the-o option is to display only matching strings:

Show non-commented lines in the / etc/fstab file, anchor lines that begin with # comments, and reverse:

The / etc/fstab file starts with a # sign, followed by a space, followed by lines of any length and characters, and the-c option counts the number of lines matched:

Add multiple blank lines to the / tmp/fstab file, starting with a # sign, followed by a space and not ending with a blank character:

Display lines without # comments, blank lines, and white space characters in the / tmp/sshd_config file:

Show the same line in the / etc/passwd file for the user name and user SHELL:

Under the / tmp directory, start with a non-letter, followed by a letter, followed by a file or directory of any length, any character:

Displays files and directories under the / tmp directory that begin with a non-letter, followed by a letter, and then end with a non-letter:

Display all files and directories under the / etc directory that begin with p and do not end with a number:

Displays the IP address in the ip an or ifconfig command, with the-E option to support extended regular expressions:

The-l option lists the files that contain strings:

-w precisely anchor the string, which is equivalent to:

The use of egrep is basically the same as that of grep, and there is no need for the escape character\ to look more concise:

This is the end of this article on "how to use regular expressions in Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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

Development

Wechat

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

12
Report