Linux text processing exercises

Overview

After basic exercises for system users, you need to start practicing on text processing

Exercises for text processing scenarios

Count the number of users whose default shell is not /sbin/nologin in /etc/passwd, and display the users

Linux text processing exercises

Find the user, UID and shell type of the maximum user UID

Linux text processing exercises

Count the number of connections of each remote host IP currently connected to this machine, and sort them from largest to smallest

ss -t | sed -rn  "s/.*[[:space:]]([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}):[0-9]+[[:space:]]+$/\1/p" | sort -nr | uniq -c

Linux text processing exercises

Write the script disk.sh to display the maximum value of the current disk partition space utilization

#!/bin/bash
echo -e "当前分区空间利用率最高的值为:\c "
df -h | sed -rn  '/\dev\/sd/s#.*[^0-9]([0-9]{1,3})%.*#\1#p' | sort -nr  | head -n 1

Write the script systeminfo.sh to display the current host information, including host name, IPv4 address, operating system version, kernel version, CPU model, memory size, hard disk size

#!/bin/bash
mem=`cat /proc/meminfo  | sed -rn 's/MemTotal:[^0-9]+([0-9]+).*/\1/p'`
echo -e "当前主机的系统信息"
echo -e "主机名:\t\t $(hostname)"
echo -e "IPV4地址:\t $(hostname -I)"
echo -e "操作系统版本:\t $(cat /etc/redhat-release)"
echo -e "当前内核版本:\t `uname -r`"
echo -e "当前CPU型号:\t `lscpu | sed -rn 's/(Model name:[^a-zA-Z]+)(.*)/\2/p'`"
echo -e "当前主机内存容量:`echo $mem/1024 |bc`MB"
echo -e "当前主机硬盘容量:`lsblk | sed -rn 's/(sd[a-z][^0-9]).*/\0/p' | awk '{print$4}'`"

Linux text processing exercises

to sum up

Through the above exercises, you can view specific problems, keep debugging, and find many precautions. details make a difference.

Guess you like

Origin blog.51cto.com/15131458/2678934