grep文本处理和脚本

Posted 超级咖啡猫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了grep文本处理和脚本相关的知识,希望对你有一定的参考价值。

1、统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来

# 方法1 grep的
# 数量
[root@centos8 ~]# grep -v .*/sbin/nologin$ /etc/passwd |wc -l
6
[root@centos8 ~]# grep -cv .*/sbin/nologin$ /etc/passwd 
6
# 用户
[root@centos8 ~]# grep -v .*/sbin/nologin$ /etc/passwd |grep -o ^[^:]*
root
sync
shutdown
halt
aaa
lx

# 方法2 sed
# 数量
[root@centos8 ~]# sed -n /\\/sbin\\/nologin$/!p /etc/passwd |wc -l
6
# 用户
[root@centos8 ~]# sed -n /\\/sbin\\/nologin$/!p /etc/passwd |sed -r s#^([^:]*):.*#\\1#g 
root
sync
shutdown
halt
aaa
lx

2、查出用户UID最大值的用户名、UID及shell类型

# 方法1
[root@centos8 ~]# sort -n -t: -k3 /etc/passwd |tail -1 |cut -d: -f1,3,7
nobody:65534:/sbin/nologin
# 方法2 思路一样
[root@centos8 ~]# sort -r -n -t: -k3 /etc/passwd |head -1 |cut -d: -f1,3,7
nobody:65534:/sbin/nologin

3、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序

# 方法1 这个方法不一定通用ipv6可能有问题
[root@centos8 ~]# ss -nt |tr -s   % |tail -n +2 |cut -d% -f5 |cut -d: -f1 |sort |uniq -c |sort -nr |head -n10
      1 8.43.85.29
      1 10.0.0.1

# 方法2 
[root@centos8 ~]# ss -nt |tail -n +2 |tr -s   : |cut -d: -f6 |sort |uniq -c |sort -nr |head -n10
      1 8.43.85.29
      1 10.0.0.1

# 方法3
[root@centos8 ~]# ss -nt |tail -n+2 |tr -s   %  |cut -d% -f5 |grep -o ^[^:]* |sort  |uniq -c |sort -nr |head -10
      1 8.43.85.29
      1 10.0.0.1

# 方法4 注意这里的grep 其实后面是有个空格再结尾的 用cat -A可以看到
[root@centos8 ~]# ss -tn |tr -s   |cat -A
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process$
ESTAB 0 52 10.0.0.151:22 10.0.0.1:62123 $
CLOSE-WAIT 32 0 10.0.0.151:38404 8.43.85.29:443 $

[root@centos8 ~]# ss -tn |tr -s   |tail -n+2 |grep -o "[^[:space:]]* $" |cut -d: -f1 |sort |uniq -c |sort -nr |head -n10
      1 8.43.85.29
      1 10.0.0.1

4、编写脚本disk.sh,显示当前硬盘分区中空间利用率最大的值

#!/bin/bash
#
###########################################
#Author: lx
#Date: 2021-12-05
#ScriptsName: check_disk_max_free.sh
###########################################

printf "\\E[1;$[RANDOM%7+31]m Starting check...\\E[0m\\n"
echo

Max_disk="`df |grep ^/dev/sd* |tr -s   |cut -d   -f1,\\5 |head -n1`"

echo -e "\\E[1;$[RANDOM%7+31]m Max free disk: ${Max_disk} \\E[0m"
echo

printf "\\E[1;$[RANDOM%7+31]m End ckeck... \\E[0m\\n"

5、编写脚本 systeminfo.sh,显示当前主机系统信息,包括:主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小

#!/bin/bash
#
###########################################
#Author: lx
#Date: 2021-12-05
#ScriptsName: see_system_info.sh
###########################################

R_COLOR="\\E[1;$[RANDOM%7+31]m"
RED="${R_COLOR}"
#RED="\\E[1;31m"
GREEN="\\E[1;32m"
END="\\E[0m"

echo -e "${GREEN}======================Host system info===================${END}"
echo -e "HOSTNAME:  ${RED}`hostname`${END}"
echo -e "IPADDR:    ${RED}`ifconfig ens33 |grep -Eo ([0-9]{1,3}\\.){3}[0-9]{1,3} |head -n1`${END}"
echo -e "OSVERSION: ${RED}`cat /etc/redhat-release`${END}"
echo -e "KERNEL:    ${RED}`uname -r`${END}"
echo -e "CPU:      ${RED}`lscpu |grep  "^Model name" |tr -s   |cut -d: -f2`${END}"
echo -e "MEMORY:    ${RED}`free -h |grep Mem |tr -s   : |cut -d: -f2`${END}"
echo -e "DISK:      ${RED}`lsblk |grep ^sd |tr -s    |cut -d  -f4`${END}"
echo -e "${GREEN}===========================================================${END}"

以上是关于grep文本处理和脚本的主要内容,如果未能解决你的问题,请参考以下文章

Linux使用之grep,shell脚本(一)

linux生产文本处理掌握这些就够了:awk,gawk,sed,grep,sort

linux生产文本处理掌握这些就够了:awk,gawk,sed,grep,sort

Shell脚本学习指南 [ 第四章 ] 查找与替换文本处理工具

面试题:shell脚本编程中grepsedawk命令详解

文本处理工具和正则表达式shell脚本编程基础-第四周