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

Shell script-regular expression

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

The concept of regular expression

Regular expression: use a single string to describe and match a series of strings that conform to a syntactic rule

Composed of ordinary characters and special characters, generally used in script programming and text editors, such as php, python, she, etc., abbreviated to regex, regexp, used to retrieve and replace text that matches the pattern, with powerful text matching function

Be able to process this article quickly and efficiently in the text ocean

Basic regular expression

The string expression method of regular expression can be divided into basic regular expression and extended regular expression according to different degree of rigor and function. The underlying regular expression is the most basic part of a commonly used regular expression. In the common file processing tools in Linux systems, grep and sed support basic regular expressions, while egrep and awk support extended regular expressions.

The metacharacter summary $matches the end of the input string. If the Multiline property of the RegExp object is set, "$" also matches'\ n'or'\ r'. To match the "$" character itself, use "\ $". Matches any single character except "\ r\ n" marks the next character as a special character, literal character, backward reference, octal escape character. For example,'n 'matches the character "n". '\ n' matches the newline character. The sequence'\ 'matches "\", while'\ ('matches "(" * matches the previous subexpression zero or more times. To match the "*" character, use the "\ *" [] character collection. Matches any of the characters contained. For example, "[abc]" can match the set of "a" [^] assigned characters in "plain". Matches any character that is not included. For example, "[^ abc]" can match any range of alphabetic [n1-n2] characters in "plin" in "plain". Matches any character in the specified range. For example, "[a Musz]" can match any lowercase character in the range of "a" to "z". Note: the range of characters can be represented only if the hyphen (-) is within the character group and occurs between two characters; if it appears at the beginning of the character group, it can only indicate that the hyphen itself {n} n is a non-negative integer. Match determined n times. For example, "o {2}" does not match the "o" in "Bob", but can match the two o {n,} n in "food" is a non-negative integer, matching at least n times. For example, "o {2,}" does not match "o" in "Bob", but does match all o in "foooood". " O {1,} "is equivalent to" o + "." O {0,} "is equivalent to" o * "{n CONFIGsed m} m} m and n are non-negative integers, where n $CONFIGsed-I-e'/ ^ local_enable/s/NO/YES/g'-e'/ ^ write_enable/s/NO/YES/g' $CONFIG grep" listen "$CONFIG | | sed-I'$alisten=YES' $CONFIG# starts the vsftpd service and is set to run the systemctl restart vsftpdsystemctl enable vsftpdawk tool automatically after boot.

In Linux/UNIX system, awk is a powerful editing tool, which reads input text line by line, searches according to the specified matching pattern, formats and outputs or filters the content that meets the requirements, and can achieve quite complex text operations without interaction. It is widely used in Shell scripts to complete a variety of automatic configuration tasks.

1. Common usage of awk

Typically, the command format used by awk is as follows, where single quotation marks and curly braces "{}" are used to set the processing action on the data. Awk can process the target file directly or through the "- f" read script.

Awk option 'mode or condition {edit instruction}' file 1 file 2 "/ filter and output contents of file symbol condition awk-f script file 1 file 2" / / call edit instructions from the script to filter and output content

If you need to find out the user name, user ID, group ID and other columns of / etc/passwd, execute the following awk command

[root@localhost ~] # awk-F':'{print $1 bindaemon 5}'/ etc/passwdroot 0 rootbin 1 bindaemon 2 daemon....// omission part awk contains several special built-in variables (which can be used directly) as follows: FS: specifies the field delimiter for each line of text, which defaults to spaces or tab stops. NF: the number of fields in the rows currently being processed. NR: the line number (ordinal) of the currently processed row. $0: the entire line content of the currently processed row. $n: the nth field (nth column) of the currently processed row. FILENAME: the name of the file being processed. RS: data records are separated. The default is\ n, that is, one record per behavior. Usage example

1) output text awk'{print} 'test.txt / / by line, equivalent to cat test.txtawk' {print $0} 'test.txt / / output all content, equivalent to cat test.txtawk' NR==1,NR==3 {print} 'test.txt / / output line 1' 3 content awk'(NR > = 1) & & (NR)

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: 208

*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