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