Linux 练习 - 文本处理工具和正则表达式

本文提供了一系列Linux系统管理的实用命令技巧,涵盖了网络配置、磁盘空间管理、用户管理、文件权限查看、网络连接分析等关键操作,适用于系统管理员和高级用户。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、找出 ifconfig “网卡名” 命令结果中本机的 IPv4 地址
[yinxd@centos7 ~]$ ifconfig eth0 | head -2 | tail -1 | tr -s " " | cut -d " " -f 3
2、查出分区空间使用率的最大百分比值
[yinxd@centos7 ~]$ df | tr -s ' ' % | cut -d % -f 5 | sort -nr | head -1
3、查出用户 UID 最大值的用户名、UID 及 shell 类型
[yinxd@centos7 ~]$ getent passwd | cut -d : -f 1,3,7 | sort -t: -k2 -nr | head -1
4、查出 /tmp 的权限,以数字方式显示
[yinxd@centos7 ~]$ stat /tmp | head -4 | tail -1 | cut -d / -f 1 | cut -d '(' -f 2

或 [yinxd@centos7 ~]$ stat -c %a /tmp
5、统计当前连接本机的每个远程主机 IP 的连接数,并按从大到小排序
[yinxd@centos7 ~]$ netstat -nt | grep tcp | tr -s ' ' | cut -d ' '  -f 5 | cut -d : -f 1 | sort -nr | uniq -c
6、显示 /proc/meminfo 文件中以大小 s 开头的行(要求:使用两种方法)
  • 方法1
[yinxd@centos7 ~]$ cat /proc/meminfo | grep '^[Ss]'
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:             18812 kB
Slab:             123340 kB
SReclaimable:      67224 kB
SUnreclaim:        56116 kB
  • 方法2
# -i 参数为忽略大小写,s可换为S
[yinxd@centos7 ~]$ grep -i  "^s" /proc/meminfo
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:             18812 kB
Slab:             123340 kB
SReclaimable:      67224 kB
SUnreclaim:        56116 kB
7、显示 /etc/passwd 文件中不以/bin/bash结尾的行
# -v 为取反,即未被匹配到的行
[yinxd@centos7 ~]$ grep -v '/bin/bash$' /etc/passwd
8、显示用户 rpc 默认的 shell 程序
[yinxd@centos7 ~]$ getent passwd rpc | grep -o "/[[:alpha:]]\+/[[:alpha:]]\+$"
/sbin/nologin
9、找出 /etc/passwd 中的两位或三位数
[yinxd@centos7 ~]$ getent passwd | grep "\<[0-9]\{2,3\}\>"
10、显示 CentOS7 的 /etc/grub2.cfg 文件中,至少以一个空白字符开头的且后面有非空白字符的行
[root@centos7 /]# cat /etc/grub2.cfg | grep "^[[:blank:]]\+[^[:blank:]]"
11、找出 “netstat -tan” 命令结果中以 LISTEN 后跟任意多个空白字符结尾的行
[root@centos7 /]# netstat -tan | grep "LISTEN[[:blank:]]\+"
12、显示 CentOS7 上所有 UID 小于 1000 以内的用户名和 UID
[yinxd@centos7 ~]$ getent passwd | cut -d: -f 1,3 | grep "\<[0-9]\{1,3\}\>"
13、添加用户 bash、testbash、basher、sh、nologin(其 shel l为 /sbin/nologin ),找 出 /etc/passwd 用户名和 shell 同名的行
[root@centos7 /]# useradd bash -s /sbin/nologin
[root@centos7 /]# useradd testbash -s /sbin/nologin
[root@centos7 /]# useradd basher -s /sbin/nologin
[root@centos7 /]# useradd sh -s /sbin/nologin
[root@centos7 /]# useradd nologin -s /sbin/nologin
[yinxd@centos7 ~]$ getent passwd | grep  "^\([^:]\+\):.*\1$"
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:1008:1008::/home/nologin:/sbin/nologin
14、利用 df 和 grep,取出磁盘各分区利用率,并从大到小排序
[yinxd@centos7 ~]$ df | grep -o "[0-9]\+%" | sort -nr
15、显示三个用户root、yinxd、solin的UID和默认shell
[yinxd@centos7 ~]$ getent passwd | grep "\(^root\)\|\(^yinxd\)\|\(^solin\)" | cut -d : -f 1,3,7
root:0:/bin/bash
yinxd:1003:/bin/bash
solin:1009:/bin/bash
16、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行
[yinxd@centos7 ~]$ grep "^[[:alpha:]_]\+(" /etc/rc.d/init.d/functions
17、使用egrep取出/etc/rc.d/init.d/functions中其基名
[yinxd@centos7 ~]$ echo /etc/rc.d/init.d/functions | egrep -o "[[:alpha:]]+$"
functions
18、使用egrep取出上面路径的目录名
[yinxd@centos7 ~]$ echo /etc/rc.d/init.d/functions | egrep -o ".*/"
/etc/rc.d/init.d/
19、统计last命令中以root登录的每个主机IP地址登录次数
[yinxd@centos7 ~]$ last | grep root | egrep -o "([0-9.]+){3,}" | sort -nr | uniq -c
     41 10.10.10.1
20、利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255
0-9: "\<[0-9]\>"
10-99:"\<[1-9][0-9]\>"
100-199:"\<1[0-9][0-9]\>"
200-249:"\<2[0-4][1-9]\>"
250-255:"\<25[0-5]\>"
21、显示ifconfig命令结果中所有IPv4地址
[yinxd@centos7 ~]$ ifconfig | egrep -o "(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"
10.10.10.10
255.255.255.0
10.10.10.255
192.168.73.128
255.255.255.0
192.168.73.255
127.0.0.1
255.0.0.0
22、将此字符串:welcome to magedu linux 中的每个字符去重并排序,重复次数多的排到前面
[yinxd@centos7 ~]$ echo welcome to magedu linux | grep -o "[[:alpha:]]" | sort | uniq -c | sort -nr
      3 e
      2 u
      2 o
      2 m
      2 l
      1 x
      1 w
      1 t
      1 n
      1 i
      1 g
      1 d
      1 c
      1 a
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值