一.第一部分 vim
1、在vim中設置tab縮進為4個字符
:set enpendtab
:set tabstop=4
2、復制/etc/rc.d/init.d/functions文件至/tmp目錄,替換/tmp/functions文件中的/etc/sysconfig/init為/var/log
[root@centos8 ~]# cp /etc/init.d/functions /tmp
[root@centos8 ~]# vim /tmp/functions
:%s#/etc/sysconfig/init#/var/log#
3、刪除/tmp/functions文件中所有以#開頭,且#后面至少有一個空白字符的行的行首的#號
:%s@^# @@
二.第二部分 文本處理工具
1、找出ifconfig“網卡名”命令結果中本機的IPv4地址
[root@centos8 ~]# ifconfig eth0 | tr -s " " |cut -d " " -f 3| head -2 |tail -1
10.0.0.150
2、查出分區空間使用率的最大百分比值
[root@centos8 ~]# df | tr -s " " "%" |cut -d% -f 5 |tail -n +2 |sort -nr |head -1
21
3、查出用戶UID最大值的用戶名、UID及shell類型
[root@centos8 ~]# cat /etc/passwd |sort -t: -k3 -nr | cut -d: -f 1,3,7 |head -1
nobody:65534:/sbin/nologin
4、查出/tmp的權限,以數字方式顯示
[root@centos8 ~]# stat /tmp |tr -s " " | cut -d" " -f 2 |head -4 |tail -1 |cut -d "/" -f1 |cut -d"(" -f2
1777
5、統計當前連接本機的每個遠程主機IP的連接數,並按從大到小排序
[root@centos8 ~]# ss -nt |tail -n +2 |tr -s ' ' : |cut -d: -f6 |sort |uniq -c |sort -nr |head -2
7 10.0.0.1
2 10.0.0.7
三.第三部分 正則表達式
1、顯示/proc/meminfo文件中以大小s開頭的行(要求:使用兩種方法)
#只匹配大小s開頭
[root@centos7-5 ~]# cat /proc/meminfo|grep -i "^ss*"
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 7792 kB
Slab: 84384 kB
SReclaimable: 51700 kB
SUnreclaim: 32684 kB
[root@centos7-5 ~]# cat /proc/meminfo|grep -i "^ss\?"
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 7792 kB
Slab: 84384 kB
SReclaimable: 51700 kB
SUnreclaim: 32684 kB
[root@centos7-5 ~]# cat /proc/meminfo|grep -i "^[sS]"
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 7792 kB
Slab: 84384 kB
SReclaimable: 51700 kB
SUnreclaim: 32684 kB
#匹配大小s開頭整行
[root@centos8 ~]# grep -E "^(s|S).*" /proc/meminfo
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 7792 kB
Slab: 84384 kB
SReclaimable: 51700 kB
SUnreclaim: 32684 kB
[root@centos8 ~]# grep "^\(S\|s\).*" /proc/meminfo
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 7792 kB
Slab: 84364 kB
SReclaimable: 51700 kB
SUnreclaim: 32664 kB
[root@centos8 ~]# grep -i "^s.*" /proc/meminfo
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 7792 kB
Slab: 84384 kB
SReclaimable: 51700 kB
SUnreclaim: 32684 kB
[root@centos8 ~]# grep '^[Ss].*' /proc/meminfo
SwapCached: 0 kB
SwapTotal: 2097148 kB
SwapFree: 2097148 kB
Shmem: 7792 kB
Slab: 84384 kB
SReclaimable: 51700 kB
SUnreclaim: 32684 kB
2、顯示/etc/passwd文件中不以/bin/bash結尾的行
[root@centos8 ~]# grep -v '/bin/bash$' /etc/passwd
3、顯示用戶rpc默認的shell程序
[root@centos8 ~]# grep -w '^rpc' /etc/passwd |cut -d: -f7
/sbin/nologin
[root@centos8 ~]# grep -w '^rpc' /etc/passwd |awk -F: '{print $NF}'
/sbin/nologin
[root@centos8 ~]# awk -F: '/rpc\>/{print $NF}' /etc/passwd
/sbin/nologin
[root@centos8 ~]# awk -F: '/^rpc\>/{print $NF}' /etc/passwd
/sbin/nologin
[root@centos8 ~]# sed -rn '/rpc\>/s#.*[^/]((/.*){2})#\1#p' /etc/passwd
/sbin/nologin
[root@centos8 ~]# sed -rn '/^rpc\>/s#.*[^/]((/.*){2})#\1#p' /etc/passwd
/sbin/nologin
4、找出/etc/passwd中的兩位或三位數
[root@centos8 ~]# grep -o '[0-9]\{2,3\}' /etc/passwd
5、顯示CentOS 7的/etc/grub2.cfg文件中,至少以一個空白字符開頭的且后邊有非空白字符的行
[root@centos8 ~]# cat /etc/grub2.cfg | grep '^[[:space:]]\+'
6、找出“netstat -tan”命令結果中以LISTEN后跟任意多個空白字符結尾的行
[root@centos8 ~]# netstat -tan |grep 'LISTEN[[:space:]]\+$'
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN
tcp6 0 0 :::111 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 ::1:6010 :::* LISTEN
7、顯示CentOS 7上所有UID小於1000以內的用戶名和UID
[root@centos8 ~]# cut -d: -f1,3 /etc/passwd|grep '^.*\:[0-9]\{1,3\}$'
8、添加用戶bash、testbash、basher、sh、nologin(其shell為/sbin/nologin),找出/etc/passwd用戶名和shell同名的行
[root@centos8 ~]# useradd -s /sbin/nologin bash
[root@centos8 ~]# getent passwd bash
bash:x:1001:1001::/home/bash:/sbin/nologin
[root@centos8 ~]# useradd -s /sbin/nologin testbash
[root@centos8 ~]# getent passwd testbash
testbash:x:1002:1002::/home/testbash:/sbin/nologin
[root@centos8 ~]# useradd -s /sbin/nologin sh
[root@centos8 ~]# getent passwd sh
sh:x:1003:1003::/home/sh:/sbin/nologin
[root@centos8 ~]# useradd -s /sbin/nologin nologin
[root@centos8 ~]# getent passwd nologin
nologin:x:1004:1004::/home/nologin:/sbin/nologin
[root@centos8 ~]# grep '\(^.*\):.*\1$' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
nologin:x:1004:1004::/home/nologin:/sbin/nologin
9、利用df和grep,取出磁盤各分區利用率,並從大到小排序
[root@centos8 ~]# df |grep -o '[0-9]\{1,3\}%'|sort -nr
21%
5%
2%
1%
1%
1%
0%
0%
0%
四.第四部分 擴展正則表達式
1、顯示三個用戶root、mage、wang的UID和默認shell
[root@centos8 ~]# cut -d: -f1,3,7 /etc/passwd | grep -e '^root' -e 'mage' -e 'wang' |cut -d: -f2,3
0:/bin/bash
1001:/bin/bash
1002:/bin/bash
2、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下划線)后面跟一個小括號的行
[root@centos8 ~]# cat /etc/init.d/functions | grep -E '^[a-z_]+\(.*'
checkpid() {
__kill_pids_term_kill_checkpids() {
__kill_pids_term_kill() {
__pids_var_run() {
__pids_pidof() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
echo_success() {
echo_failure() {
echo_passed() {
echo_warning() {
update_boot_stage() {
success() {
failure() {
passed() {
warning() {
action() {
strstr() {
is_ignored_file() {
is_true() {
is_false() {
apply_sysctl() {
3、使用egrep取出/etc/rc.d/init.d/functions中其基名
[root@centos8 ~]# echo /etc/init.d/functions | egrep -w '\b[a-z]{9}\b'
/etc/init.d/functions

4、使用egrep取出/etc/rc.d/init.d/functions的目錄名
[root@centos8 ~]# echo /etc/init.d/functions | egrep -w '^/\b[a-z./]{10}\b'
/etc/init.d/functions

5、統計last命令中以root登錄的每個主機IP地址登錄次數
[root@centos8 ~]# last | grep '^root' | tr -s " "|cut -d " " -f3 |sort|uniq -c
14 10.0.0.1
3 10.0.0.150
1 10.0.0.152
1 10.0.0.153
1 10.0.0.154
6、利用擴展正則表達式分別表示0-9、10-99、100-199、200-249、250-255
[root@centos8 ~]# seq 1 255 |grep -E '^[0-9]{1}$'
[root@centos8 ~]# seq 1 255 |grep -E '^[0-9]{2}$'
[root@centos8 ~]# seq 1 255 |grep -E '[0-9]{3}$' |grep '^1[0-9].*'
[root@centos8 ~]# seq 1 255 |grep -E '[0-9]{3}$' |grep '^2[0-9].*' | grep '^2[^5][0-9]'
[root@centos8 ~]# seq 1 255 |grep -E '[0-9]{3}$' |grep '^2[0-9].*' | grep '^2[^01234][0-9]'
7、顯示ifconfig命令結果中所有IPv4地址
[root@centos8 ~]# ifconfig eth0 |grep 'netmask' | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}'|head -1
10.0.0.151
8、將字符串:welcome to magedu linux中的每個字符去重並排序,重復次數多的排到前面
[root@centos8 ~]# echo "welcome to magedu linux" | tr -d " " | grep -o "[a-z]" |sort |uniq -c|sort -nr