"That year that man that Linux" "Regular expression" the first glimpse of the second problem

Source: Internet
Author: User

Starting from the Linux learning process to detect the effect of learning, students recommend two regular expressions of the topic. These two topics are not many words, but it has triggered a piece of my thinking. operating Environment CentOS6.7 2.6.32-573.22.1.el6.x86_64

Now the title is organized as follows:

"The first "

Use a regular expression with the SED command to replace the first word in/etc/passwd with the last word. For example:

Sc:x:891:809::/home/sc:/bin/bash #操作之前bash: X:891:809::/home/sc:/bin/sc #操作之后

" Second "

Use regular expressions with the SED command to replace the first set of numbers in the/etc/passwd with the last word.

Sc:x:891:809::/home/sc:/bin/bash #操作之前sc: x:bash:808::/home/sc:/bin/890 #操作之后


Answer

First look at the first.

First, our main goal is to extract the first word and the last word, observe the character of the word, the pure letter form, so it can be expressed as (^[a-za-z]*) and ([a-za-z]*$);

The second step, the rest of the part as a whole , observe the characteristics, we will find the remainder is two ":" Between the part, so you can think of the colon beginning, the end of the colon, and then, in order to complete the expression of this part, we can use ([^a-za-z].*[^ A-za-z]) (so that three parts are finished);

Finally, according to the SED command replacement rules, auxiliary later to the reference "\3\2\1", complete the operation! Paste the following command line:

[[email protected] ~]# sed-r ' s# (^[a-za-z]*) ([^a-za-z].*[^a-za-z]) ([a-za-z]*$] #\3\2\1#g '/etc/passwdbash:x:0:0:root :/ROOT:/BIN/ROOTNOLOGIN:X:1:1:BIN:/BIN:/SBIN/BIN......BASH:X:889:807::/HOME/SA:/BIN/SABASH:X:890:808::/HOME/SB :/bin/sbbash:x:891:809::/home/sc:/bin/sc[[email protected] ~]#


Summary: We analyze the contents of each line of the file, the results found that according to the topic can be divided into a line of text, "word-two non-letter parts-the word". According to this pattern, we use the Word as a force point , the statement is perfectly separated, the shell does not recognize the error.


Second, this topic is the upgrade version of the previous question, at first glance can be used as the first problem of the pattern to solve problems. Let's start with an overall analysis and look for ideas.


Overall analysis: According to the requirements of the topic to change the first set of numbers and the last word, from the analysis of the previous question, we can choose from the number and the word two points to start the experiment . We can look at the following pattern "non-numeric part-the first set of numbers-non-alphabetic parts-the last word". The order in which the regular expressions are parsed from the shell, the above pattern is correct and can represent a single line of content. Paste the following command line:

[Email protected] ~]# sed-r ' s# (^[^0-9]*) ([0-9]*] ([^a-za-z].*[^a-za-z]) ([a-za-z]*$) #\1\4\3\2#g '/etc/passwdroot:x: bash:0:root:/root:/bin/0bin:x:nologin:1:bin:/bin:/sbin/1......sa:x:bash:807::/home/sa:/bin/889sb:x:bash:808::/ Home/sb:/bin/890sc:x:bash:809::/home/sc:/bin/891[[email protected] ~]#


Inspire

When learning to use regular expressions, analyze the requirements and select the key points separating the line information to complete the match.

This article from "Passionie" blog, reproduced please contact the author!

"That year that man that Linux" "Regular expression" first glimpse of the second problem

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.