栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

linux17 考试题详解和while

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

linux17 考试题详解和while

linux17 考试题详解和while 1、找出/proc/meminfo文件中以s开头的行,至少用三种方式忽略大小写
#第一种方式
[root@localhost ~]# egrep -i "^s" /proc/meminfo 
SwapCached:            0 kB
SwapTotal:       1572860 kB
SwapFree:        1572860 kB
Shmem:              7688 kB
Slab:              42668 kB
SReclaimable:      20892 kB
SUnreclaim:        21776 kB
#第二种方式
[root@localhost ~]# egrep "^s|^S" /proc/meminfo 
SwapCached:            0 kB
SwapTotal:       1572860 kB
SwapFree:        1572860 kB
Shmem:              7688 kB
Slab:              42668 kB
SReclaimable:      20892 kB
SUnreclaim:        21776 kB
#第三种方式
[root@localhost ~]# egrep "^[sS]" /proc/meminfo 
SwapCached:            0 kB
SwapTotal:       1572860 kB
SwapFree:        1572860 kB
Shmem:              7688 kB
Slab:              42668 kB
SReclaimable:      20892 kB
SUnreclaim:        21776 kB
#第四种方式   awk 
[root@localhost ~]# awk '/^[sS]/' /proc/meminfo 
SwapCached:            0 kB
SwapTotal:       1572860 kB
SwapFree:        1572860 kB
Shmem:              7688 kB
Slab:              42668 kB
SReclaimable:      20892 kB
SUnreclaim:        21776 kB
#第五种方式  sed   -n 取消默认输出  -r使用正则   
[root@localhost ~]# sed -n -r "/^[sS]/p" /proc/meminfo 
SwapCached:            0 kB
SwapTotal:       1572860 kB
SwapFree:        1572860 kB
Shmem:              7688 kB
Slab:              42668 kB
SReclaimable:      20892 kB
SUnreclaim:        21776 kB
3、找出/etc/init.d/function文件下包含小括号的行
[root@localhost ~]# egrep "[()]" /etc/init.d/functions 
4、输出指定目录的基名
基名指的是当前所在地方的上一层文件夹
[root@localhost 123]# pwd | awk -F/ '{print $NF}' 
5、找出网卡信息中包含的数字
[0-9]+  0-9一个或者多个
[root@localhost 456]# egrep -o "[0-9]+" /etc/sysconfig/network-scripts/ifcfg-* 
6、找出/etc/passwd下每种解析器的用户个数
比如bash这个解析器的用户有几个
[root@localhost 456]# awk -F: '{arr[$NF]++} END{for(i in arr) {printf "%-20s%dn",i,arr[i]}}' /etc/passwd
/bin/sync           1
/bin/bash           6
/sbin/nologin       17
/sbin/halt          1
/sbin/shutdown      1
7、过滤网卡中的ip,用三种方式实现
#第一种方式
[root@localhost 456]# ip a | egrep -o "([0-9]{1,3}.){1,3}[0-9]{1,3}" 
127.0.0.1
192.168.15.222
192.168.15.255
172.16.1.100
172.16.15.255
#第二种方式
[root@localhost 456]# ip a | sed -n -r "/([0-9]{1,3}.){1,3}[0-9]{1,3}/p"
    inet 127.0.0.1/8 scope host lo
    inet 192.168.15.222/24 brd 192.168.15.255 scope global eth0
    inet 172.16.1.100/20 brd 172.16.15.255 scope global eth1
    
#第三种方式
[root@localhost 456]# ip a |awk '/([0-9]{1,3}.){3}[0-9]{1,3}/ {print $2,$4} ' | awk '{if (NR==1) {print $1} else {print $0}}'
8、搜索/etc目录下,所有的.html或.php文件中包含的main函数出现的次数
   $后面的内容查出来的路径给前面用
[root@localhost etc]# egrep -o "main" $(find /etc/ -name "*.html" -o -name "*.php") | wc -l 
9、过滤/etc/fstab中注释的行和空行
#可以看出#  和空行都被过滤掉了
[root@localhost etc]# egrep -v '^ *#|^$' /etc/fstab 
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=bda5d4f6-f6af-448a-bd08-45997f1288c7 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/sdb1 /mnt xfs defaults 0  0
#显示过滤掉的行
[root@localhost etc]# egrep  '^ *#|^$' /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Sep 18 20:04:05 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#


       #  a asd ad    asd
              # adasd
#asdasd
10、找出文件中至少有一个空格的行
[root@localhost etc]# egrep " +" /etc/fstab 
# /etc/fstab
# Created by anaconda on Sat Sep 18 20:04:05 2021
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=bda5d4f6-f6af-448a-bd08-45997f1288c7 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
       #  a asd ad    asd
/dev/sdb1 /mnt xfs defaults 0  0
              # adasd
11、过滤文件中以#开头的行,后面至少有一个空格
[root@localhost etc]# egrep "^# +" /etc/fstab 
# /etc/fstab
# Created by anaconda on Sat Sep 18 20:04:05 2021
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
12、查询出/etc目录中包含多少个root
-R递归查询  [root@localhost etc]# egrep -Ro 'root' /etc/ | wc -l
13、查询出所有的qq邮箱
[root@localhost tmp]# egrep  "[0-9A-Za-z]+@(qq.com)" 3.txt 
12341213123@qq.com
14、查询系统日志(/var/log/message)中所有的error
[root@localhost tmp]# egrep -i "error" /var/log/messages
Oct 11 09:14:25 localhost kernel: BERT: Boot Error Record Table support is disabled. Enable it by using bert_enable as kernel parameter.
16、删除一个文件中的所有数字
#错误的   d直接把那一行全部删除了 不是我们要的结果
[root@localhost tmp]# sed -r "/[0-9]/d" 1.txt 
我没得数字

#正确的 s///g 可以把数字替换成为空,全局
[root@localhost tmp]# sed -r "s/[0-9]//g" 1.txt 
I am Poe,my qq is 
adadw
我有数字
我没得数字
qwe
17、显示奇数行
[root@localhost tmp]# awk -F: 'NR%2{print NR,$0}' /etc/passwd
1 root:x:0:0:root:/root:/bin/bash
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
18、删除passwd文件中以bin开头的行到nobody开头的行
[root@localhost tmp]# sed -r  "/^bin/,/^nobody/d" /etc/passwd
20、每隔5行打印一个空格行
NR%5不等于0时 true   等于0 为false    加俩换行符是因为有一个默认的n
[root@localhost tmp]# awk -F: '{if (NR%5) {print $0}else{printf "%s nn",$0 }}' /etc/passwd
21、不显示指定字符的行
[root@localhost tmp]# grep -v "指定字符" /etc/passwd
22、将文件中1到5行中aaa替换成AAA
[root@localhost tmp]# sed -r '1,5s/aaa/AAA/g' 2.txt 
q
AAAw
erAAA
r
q
we
:wq
aaa
aaa
23、显示用户id为奇数的行
#用户id第三列所以$3 ,取余2  不等于0则是奇数 print输出这行内容
[root@localhost tmp]# awk -F: '{if ($3%2) {print $0}}' /etc/passwd
25、统计nginx日志中访问量(ip唯独计算)
查找ip 然后-c查看多少行,他只要访问量  一行代表一个访问量
[root@localhost tmp]# egrep -c "([0-9]{1,3}.){3}[0-9]{1,3}" /var/log/nginx/access.log
662
26、统计访问nginx前10的ip
[root@localhost tmp]# egrep -o "([0-9]{1,3}.){3}[0-9]{1,3}" /var/log/nginx/access.log | sort|uniq -c | sort -rn|head
    493 106.12.223.189
     24 106.12.223.203
     24 106.12.223.200
     22 106.12.223.202
     19 106.12.223.204
     17 198.98.60.202
     13 45.146.164.110
     13 106.12.223.201
      9 134.122.56.69
      6 209.141.56.41
sort参数作用
从小到大-n按照数值大小进行排序(默认按照第一个字符排序)
从大到小-r和上面的相反,从大到小排序,也叫倒序,如果是数值排序记得加上-n
因为默认按照第一个字符排序
uniq:只能处理相邻的重复,就是挨着的两个参数作用
前面显示重复次数,后面显示内容-c打印出重复次数
head:从头开始读行默认十行参数作用
读取行内容-n读取多少行
27、统计nginx日志中的访问人数
sort 排序       uniq筛选 
[root@localhost tmp]# egrep -o "([0-9]{1,3}.){3}[0-9]{1,3}" /var/log/nginx/access.log | sort|uniq -c| wc -l
之前剩下的awk里面的while循环
while (判断条件){}
案例一把etc/passwd每一行都重复打印三遍
[root@localhost tmp]# awk -F: '{i=1;while(i<=3){print $0;i++}}' /etc/passwd
                             先声明i=1, while 如果i<=3 print 这一行内容 然后i++
;	#多条命令分割

案例二 要求把/etc/passwd种第十行的每一列都打印出来
[root@localhost tmp]# awk -F: 'NR==10 {i=0;while(i<=NF) {print $i;i++}}' /etc/passwd
operator:x:11:0:operator:/root:/sbin/nologin
operator
x
11
0
operator
/root
先加个条件 NR==10  选定了第十行, 定义i=0 ,while  i
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/312708.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号