Linux三剑客考试题

Linux三剑客考试题

1、找出/proc/meminfo文件中以s开头的行,至少用三种方式忽略大小写

[root@kubernetes nginx]# grep -iE '^s' /proc/meminfo
[root@kubernetes nginx]# grep -E '^[sS]' /proc/meminfo
[root@kubernetes nginx]# awk '/^[sS]/' /proc/meminfo
[root@kubernetes nginx]# sed -n -r '/^[sS]/p' /proc/meminfo

3、找出/etc/init.d/functions文件下包含小括号的行

[root@kubernetes ~]# grep -E '[()]' /etc/init.d/functions

4、输出指定目录的基名

[root@kubernetes network-scripts]# pwd | awk -F/ '{print $NF}'

5、找出网卡信息中包含的数字


[root@localhost network-scripts]# grep -oE '[0-9]+' /etc/sysconfig/network-scripts/ifcfg-*

6、找出/etc/passwd下每种解析器的用户个数

[root@localhost network-scripts]# awk -F: '{arr[$NF]++}END{for(i in arr){printf "%-15s %s \n",i,arr[i]}}' /etc/passwd

7、过滤网卡中的ip,用三种方式实现
[root@localhost ~]# ip a | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}'
[root@localhost ~]# ip a | sed -rn '/([0-9]{1,3}\.){3}[0-9]{1,3}/p'
[root@localhost ~]# ip a | awk '/([0-9]{1,3}\.){3}[0-9]{1,3}/{print $2,$4}' | awk '{if (NR==1){print $1}else {print $1,$2}}'

8、搜索/etc目录下,所有的.html或.php文件中包含的main函数出现的次数


[root@localhost etc]# grep -oE 'main' $(find /etc/ -name "*.html" -o -name "*.php") | wc -l

9、过滤/etc/fstab中注释的行和空行
[root@localhost etc]# grep -vE '^$|^ *#' /etc/fstab

10、找出文件中至少有一个空格的行
[root@localhost etc]# grep -E " +" /etc/fstab

11、过滤文件中以#开头的行,后面至少有一个空格

[root@localhost etc]# grep -E '^# +' /etc/fstab

12、查询出/etc目录中包含多少个root

[root@localhost etc]# egrep -oR 'root' /etc/ | wc -l

13、查询出所有的qq邮箱
[root@localhost ~]# egrep "[0-9a-zA-Z]+@qq.com" 1.txt

14、查询系统日志(/var/log/message)中所有的error

[root@localhost ~]# grep -iE 'error' /var/log/messages

16、删除一个文件中的所有数学
[root@localhost ~]# sed -r 's/[0-9]//g' 1.txt

17、显示奇数行
[root@localhost ~]# awk -F: 'NR%2{print $0}' /etc/passwd


18、删除passwd文件中以bin开头的行到nobody开头的行
[root@localhost ~]# sed -r "/^bin/,/^nobody/d" /etc/passwd

20、每隔5行打印一个空格行
[root@localhost ~]# awk -F: '{if(NR%5==0){printf "%s\n\n",$0}else{print $0}}' /etc/passwd

21、不显示指定字符的行

grep -vE 'root' /etc/passwd

22、将文件中1到5行中aaa替换成AAA
[root@localhost ~]# sed -r '1,5s/aaa/AAA/g' 2.txt

23、显示用户id为奇数的行
[root@localhost ~]# awk -F: '{if($3%2){print $0}}' /etc/passwd

25、统计nginx日志中访问量(ip维度计算)
[root@kubernetes ~]# grep -cE "([0-9]{1,3}\.){3}[0-9]{1,3}" /var/log/nginx/access.log
[root@kubernetes ~]# grep -cE "^([0-9]{1,3}\.){3}[0-9]{1,3}" /var/log/nginx/access.log

26、统计访问nginx前10的ip



[root@kubernetes ~]# grep -oE "^([0-9]{1,3}\.){3}[0-9]{1,3}" /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -10




知识储备:

sort : 处理排序(默认,按照第一个字符进行排序)

-n : 按照数值得大小进行排序
-r : 倒序排序

uniq :处理重复(只能够处理相邻的重复)

-c : 打印出重复次数

head :从文本头部开始读数据(默认只读前10行)

-n : 读前n行

27、统计nginx日志中的访问人数

[root@kubernetes ~]# grep -oE "^([0-9]{1,3}\.){3}[0-9]{1,3}" /var/log/nginx/access.log | awk '{arr[$0]++}END{print length(arr)}'

[root@kubernetes ~]# grep -oE "^([0-9]{1,3}\.){3}[0-9]{1,3}" /var/log/nginx/access.log | sort | uniq -c | wc -l


知识储备:

知识储备:
sort 处理排序(默认,按照第一个字符进行排序)
-n :按照数值的大小进行排序
-r : 倒序排序
uniq :  处理重复(只能够处理相邻的重复)
-c : 打印出重复次数
head 从文本头部开始读数据(默认只读前十行)
-n : 读前n行

 

posted @ 2021-10-11 18:47  毛瑞杰  阅读(304)  评论(0)    收藏  举报