Linux Learning-Regular expressions

Source: Internet
Author: User
Tags egrep

Regular Expressions- for system administrators, to process a lot of information every day, there are a lot of useless information, you can filter the information through regular expressions, so-called regular expression, is through the arrangement of some special characters, to search, replace, delete one or more lines of string.

First, text filtering tool grep

Format: grep + [options] + match content + [file]

--color=auto keyword highlighting

- v displays rows that are not matched to

-i ignores case

- n Displays the matching line number cat/etc/passwd |grep-n root

- C Displays the number of matching rows

- o Show only matched strings

- q silent mode, does not display any information

Can be used when we are concerned about whether the execution is successful or not, when we don't care about the results.

grep-q shuguo/etc/group && echo exist | | Echo not exist exists then output exist, not present output not exist

-A + number displays rows that match rows and next to the specified number

cat/etc/passwd |grep-a 3 root

- B + numbers show rows that match rows and numbers that are adjacent up

- C + numbers display rows that match rows and specify numbers up and down

- e Multiple options or relationships

grep-e root-e bin/etc/passwd

- w matches only the entire word

- E or egrep support for regular expressions

grep-e "Root|bin"/etc/passwd

egrep "Root|bin"/etc/passwd

- F or fgrep does not support regular expressions

Second, the regular expression

Character matching

      . match any single character

[] matches any single character in the range

[^] matches any single character outside the range

[ : Alnum:] [: Alpha:] [: Lower:] [: Upper:] [:d igit:] [:p UNCT: ]

[: blank:] spaces and tabs

[: space:] horizontal and vertical whitespace characters (a wide range than [: blank:])

number match is used after the number of characters to be specified

* match the preceding character any time, including 0 times

for example:. * Any character of any length

   \? matches the preceding character 0 or 1 times

\+ matches The preceding character at least 1 times

\{n\} matches the preceding character n times

\{m,n\} matches the preceding character at least m times, up to n times

\{,n\} matches the preceding character up to n times

\{m,\} matches the preceding character at least m times

Position anchor position where it appears, where to start and where to end

^ beginning Anchor

$ line End anchoring

For example: Match starts with #, ends with 0, and the middle content is arbitrary

grep "^#.*0$"

match begins with #, ending with 0

grep "^ #0 $"

^$ empty line

^[[:space:]]*$ blank line

\< the first anchor of the word

\> ending anchoring

Group

\ (\) binds one or more characters together as a whole

For example: grep "[Ab]\{2\}" indicates a or B matches two times

grep "\ (ab\) \{2\}" means ab this whole match two times

The content that is matched within the grouping parentheses is recorded in the variable, and the names of these variables are \1,\2 ...

\ (string1\+\ (string2\) \)

\1 means string1\+\ (string2\)

\2 says string2

\| Or

a\|b A or B

\ (c\|c\) at cat or cat

Third, the case

1. Display lines in the/proc/meminfo file that begin with size S

cat/proc/meminfo |grep "^s\|^s"

cat/proc/meminfo |grep-i ^s

grep ^[ss]/proc/meminfo

grep-e ^s-e ^s/proc/meminfo

2. Display lines in the/etc/passwd file that do not end in/bin/bash

CAT/ETC/PASSWD |grep-v "/bin/bash$"

3. Show user RPC default shell program

grep "^\<rpc\>"/etc/passwd |cut-d:-f7

grep "^rpc\>"/etc/passwd |cut-d:-f7

grep "^\ (rpc\)"/etc/passwd |cut-d:-f7

The first two results are the same as the result of the title request

Note the difference

4. Find out the two-bit or three-digit number in/etc/passwd

cat/etc/passwd |grep-o "[[:d igit:]]\{2,3\}"

cat/etc/passwd |grep-o "[[:d igit:]]\{2\}\| [[:d igit:]]\{3\}]

5. A line that displays at least one whitespace character in the CentOS7/etc/grub2.cfg file, followed by a non-whitespace character

cat/etc/grub2.cfg | grep "^[[:space:]]\+[^[:space:]"

6. Find the line that ends with ' LISTEN ' followed by any number of whitespace characters in the result of the "Netstat-tan" command

Netstat-tan | grep "listen[[:space:]]\+$"

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

cat/etc/passwd |cut-d:-f1,3 | grep "\<[0-9]\{1,3\}\>"

8, add user bash, Testbash, basher, SH, nologin (its shell is/sbin/nologin), find the/etc/passwd user name with the shell name of the line

useradd bash

Useradd Testbash

Useradd basher

Useradd SH

useradd-s/sbin/nologin Nologin

cat/etc/passwd |grep "^\ (. *\) \>.*\<\1$"

9, take advantage of DF and grep, remove the disk partition utilization, and from large to small sort

DF |grep "^/DEV/SD" |grep-o "[[:d igit:]]\{1,3\}%" |tr-d% |SORT-NR

Iv. Extending the regular expression

Egrep = Grep-e

Egrep character matching, number matching, position anchoring, grouping and grep difference is to remove the front of the symbol, in addition to \<,\>,\b unchanged

Case:

1. Display three user root, Mage, Wang's UID and default shell

cat/etc/passwd |cut-d:-f1,3,7 |grep-w "Root\|mage\|wang"

CAT/ETC/PASSWD |cut-d:-f1,3,7 |grep "^\ (root\|mage\|wang\) \>"

2. Find the line at the beginning of the/etc/rc.d/init.d/functions file that has a word (including an underscore) followed by a parenthesis

cat/etc/rc.d/init.d/functions |egrep "^[_[:alpha:]]+\ (\)"

3. Use Egrep to remove its base name in/etc/rc.d/init.d/functions

echo/etc/rc.d/init.d/functions |egrep-o "\<[[:alpha:]]*\>$"

echo/etc/rc.d/init.d/functions |egrep-o "\<[a-z]*\>$"

4. Use Egrep to remove/etc/rc.d/init.d/functions/directory name

echo/etc/rc.d/init.d/functions/|egrep-o ". */." |egrep-o ". */"

5. Count the number of logins per host IP address that was logged in as root in the last command

Last |grep "root" |tr-s "": |cut-d:-f3 |uniq-c |sort-u

6, this string: Welcome to magedu each character in Linux to go to the weight and sort, repeat the number of rows to the front

Echo Welcome to magedu Linux |grep-o. |sort |uniq-c |sort-nr


Linux Learning-Regular expressions

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.