5.文字處理工具和正則表示式
第一部分 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開頭的行(要求:使用兩種方法)
[root@centos8 ~]# cat /proc/meminfo |grep -i '^ss*'
SwapCached: 0 kB
SwapTotal: 4194300 kB
SwapFree: 4194300 kB
Shmem: 10828 kB
Slab: 119600 kB
SReclaimable: 56376 kB
SUnreclaim: 63224 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
[root@centos8 ~]# cat /proc/meminfo |grep -i '^ss\?'
SwapCached: 0 kB
SwapTotal: 4194300 kB
SwapFree: 4194300 kB
Shmem: 10828 kB
Slab: 119600 kB
SReclaimable: 56376 kB
SUnreclaim: 63224 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
[root@centos8 ~]# cat /proc/meminfo |grep '^[Ss]'
SwapCached: 0 kB
SwapTotal: 4194300 kB
SwapFree: 4194300 kB
Shmem: 10828 kB
Slab: 119600 kB
SReclaimable: 56376 kB
SUnreclaim: 63224 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 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
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