Linux Regular expressions Some of the questions are detailed

Source: Internet
Author: User

Learning Linux is almost one months, the two days to learn the regular expression, for me this novice is a bit difficult, but through these days of learning or mastered a certain regular expression of knowledge, the following to share a few of the things I did feel good problem, I will give the answer with as much detail as possible.

1. displays lines in CentOS7 's/etc/grub2.cfg file that have at least one whitespace character and that are followed by non-whitespace characters

[[email protected] ~] #grep "^[[:space:]].\+[^[:space:]"/etc/grub2.cfg

        This problem requires displaying lines in the specified directory file that begin with at least one white-space character, followed by a non-whitespace character, so the first thing we think of in this sentence is that at least one [: Space:] start ^ non-blank ^[: Space:] Well, the next thing to do is to put them together in a reasonable combination, first hit the ^[[: Space:]]represents a line that starts with a white-space character, and then.that matches the preceding white space character,.The following/+ represents a match at least one of the preceding whitespace characters, which means that at least one of the whitespace characters at the beginning of the preceding line is not limited to a maximum .[^[:space:]]indicates that a non-whitespace character exists after the desired line. Since the title does not indicate how many non-whitespace characters, it does not match, and finally on both sides with English double quotation marks, and finally add a matching file directory after the OK. (Note: [: space:] the outer bracket does not represent a single character filter .instead, it is a representation of the whitespace character, so reference[: space:]when you have to add the brackets to the outside.

2. Display the user name and UID of all system users on CentOS7

    [[Email protected] ~] #cut-D:-f1,3/etc/passwd |grep "\<[[:d igit:]]\{1,3\}\>"

The UID number of the system user in CentOS7 is 0-999, and the user fame in the first column of/etc/passwd, the UID is in the third column, so the first thing we do is to separate the first and third columns of the passwd file. Cut instruction ,- Drefers to the delimiter following the reference,:and that's the delimiter we're referencing, using it to treat the passwd file as a:multiple columns that are separated,- Frefers to the selection of the column, if you choose multiple columns, add a comma, followed by the number of columns needed. After the separation is complete, the need to do is to exclude non-system users, the UID is more than 1000 users,[[:d igit:]]represents a single number to match,\{1,3\}It means that for the previous number to match up to three times at least once, that is, a maximum of three digits, at least one number, so that the match to the user is limited to the range of uid0-999, but if this is the way you will find non-system users or display, this is why, Because we just chose the three-digit number, but when the four-digit number appears, the computer will still let us match his middle three bits, for example 2548, the computer will match 254 and 548, so there will be four digits, so then we add a\<, add a \> to the end they mean the first (ending) anchor, that is, to stop the front (back) of the not, this time again to filter out the UID more than 999 users.

3. Add User bash, Testbash, basher, SH, nologin (whose shell is/sbin/nologin), find out/etc/ passwd user name with Shell name Line

[[Email protected] ~] #cat/etc/passwd|grep "\ (^.*\) \>.*/\1$"

The first is to create the user, his command is useradd and then the space to follow the user name, and add Nologin, and then ask for it to assign the default shell, then we use such an instruction, Useradd-s/sbin/nologin n Ologin,-S is a useradd option, you can create a user at the same time, the default shell for it, the user created, the next is to find the passwd file, the user name and its own shell the same row, then how to represent these different user names, Before representing the user name, say a concept called a back reference, he means \ (\)Can be referenced again at a later time, and the number of references can be selected, for example\ ([a][b]\)indicates that multiple characters in parentheses are tied together as a single character and can be used later by\1the way to refer to this character set again, if you want to refer to several times can be 1 to another number or a regular expression of the number of times, so that it is very good to echo the requirements of the same question before and after the same requirements, then, the user name in the passwd file may be a number or uppercase and lowercase letters, we use.*to represent, plus a bracket character^to indicate that the character set in the parenthesis and the matching contents are at the beginning of the line, the user name, the end plus\>, as in the previous question, said to stop, otherwise the computer will match the user name after the matching character set content, followed by some other information, we also use.*said, plus/if not added, the computer will match the extra rows, such as user name SH user's shell is bash, will also be displayed, but this is not the subject of the request, and finally hit the last we said at the beginning\1, and then use$indicates that this is the end of the line, no, the match is not all the user name and shell the same line, the user name and the shell will appear the same information between the line.


Linux Regular expressions Some of the questions are detailed

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.