The usage of several regular expressions in Linux

Regular expression is a pattern used to match each line of input. A pattern refers to a sequence of characters. Has a powerful character search function. It is also very convenient to search and filter out the content we want.

Linux system: CentOS Linux release 8.1.1911 (Core)

1、 Find out the IPv4 address of the machine in the result of the ifconfig "network card name" command

ifconfig | head -n 2|tail -1|tr -s " "|cut -d" "-f3

2、 Find the maximum percentage value of partition space usage

df |tr -s " "|cut -d" "-f5

3、 Find the user name, UID and shell type of the maximum user UID

cat /etc/passwd | cut -d:-f1,3,7| sort -nt:-k2 |tail -n 1

4、 Check out the permissions of /tmp

stat /tmp | head -n 4|tail -n 1|cut -c10-13

5、 Display the user names and UIDs of all system users on CentOS8

cat /etc/passwd |cut -d:-f1,3| egrep -v "[0-9]{4,}"

6、 Display the UID and default shell of the three users root, linuxmi, mi (A8 instead)

cat /etc/passwd |egrep "^(root|A8)"|cut -d:-f1,3

7、 Use egrep to retrieve the qualified characters in the display file in /etc/rc.d/init.d/functions

echo /etc/rc.d/init.d/functions | egrep "[a-z]$"

8、 Use egrep to retrieve the directory name of the above path

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

9、 Count the login times of each host IP address logged in as root in the last command

10、 Use extended regular expressions to represent 0-9, 10-99, 100-199, 200-249, 250-255

echo {1..255}|egrep "\<[0-9]\>"

egrep "\<1[0-9]\>"

egrep "\<1[0-9][0-9]\>"

egrep "\<2[0-4][0-9]\>"

egrep "\<25[0-5]\>"

11、 Display all IPv4 addresses in the result of the ifconfig command

ifconfig |egrep "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}"

Simply put, a regular expression is a description of a set of text being processed.

  • Link: https://www.linuxmi.com/linux-zhengzebiaodashi.html*

Recommended Posts

The usage of several regular expressions in Linux
The usage of several regular expressions in Linux
The usage of several regular expressions in Linux
The usage of wheel in python
The usage of tuples in python
The usage of Ajax in Python3 crawler
The meaning and usage of lists in python
Detailed usage of dictionary in Python
Usage of os package in python
What is the function of adb in python
ubuntu adjusts the priority of swap swap space usage
Python implements the shuffling of the cards in Doudizhu
Detailed explanation of the usage of Python decimal module
LVM disk expansion problem of Centos7 in Linux
The consequences of uninstalling python in ubuntu, very
Introduction to the use of Hanlp in ubuntu
One article to get regular expressions in Python
The difference between CentOS and Ubuntu in Linux system
How to understand the introduction of packages in Python
Resolve the problems encountered in the linux environment under ubuntu