linux三剑客试题汇总

  • A+
所属分类:linux技术
摘要

以后遇到计算一个参数出现的次数,就想到用字典的方式来计算以后遇到计算一个参数出现的次数,就想到用字典的方式来计算

目录

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

[root@localhost ~]# grep -E '^[sS]' /proc/meminfo  [root@localhost ~]# sed -r -n '/^[sS]/p' /proc/meminfo [root@localhost ~]# awk '/^[sS]/{print $0}' /proc/meminfo [root@localhost ~]# grep -iE '^s' /proc/meminfo  

2、显示etc目录下以root,centos或者user开头的信息

[root@localhost ~]# grep -rE '^(root|centos|user)' /etc/ 

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

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

4、输出指定目录的基名

[root@localhost /etc/sysconfig]# pwd | awk -F/ '{print $NF}' 

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

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

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

{"bash": 10, "sh": 9, "zsh": 1} 数组 [root@localhost /etc/sysconfig]# awk -F: '{dic[$NF]++}END{for(i in dic){print i,dic[i]}}' /etc/passwd 

linux三剑客试题汇总
以后遇到计算一个参数出现的次数,就想到用字典的方式来计算

7、获取网卡中的ip,用三种方式实现

[root@localhost /etc/sysconfig]# ip a | grep -oE '([0-9]{1,3}.){3}[0-9]{1,3}' [root@localhost /etc/sysconfig]# ip a | sed -r -n '/([0-9]{1,3}.){3}[0-9]{1,3}/p' [root@localhost /etc/sysconfig]# ip a | awk '/([0-9]{1,3}.){3}[0-9]{1,3}/{if(NR==3){print $2}else{print $2,$4}}' 

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

[root@localhost ~]# grep -rE 'main' `find /etc/ -name "*.html" -o -name "*.conf" | xargs ` | wc -l  #  ``是优先运算其内的操作 

9、过滤掉php.ini中注释的行和空行

[root@localhost ~]# yum install php php-devel [root@localhost ~]# grep -vE '^ *;|^$' /etc/php.ini # 这个文件内的注释是; 

10、找出文件中至少有一个空格的行

[root@localhost ~]# grep -E ' +' /etc/php.ini 

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

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

12、查询出/etc目录下文件内包含多少个root

[root@localhost ~]# grep -roE 'root' /etc/ | wc -l  # grep -r是递归过滤 

13、查询出所有的qq邮箱

[root@localhost ~]# grep -E '[0-9a-zA-Z-_][email protected]' 

14、查询系统日志中所有的error

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

15、删除某文件中以s开头的行的最后一个词

[root@localhost ~]# grep -E '^s' 9.txt | sed -r 's/[0-9a-zA-Z]+$//g'  # 词的正则匹配是全部大小写和数字 

16、删除一个文件中的所有数字

[root@localhost ~]# sed -r 's/[0-9]//g' 11.txt 

17、显示奇数行

[root@localhost ~]# awk -F: 'NR%2==1{print $0}' /etc/passwd 

18、删除passwd文件中以bin开头的行到nobody开头的行

[root@localhost ~]# sed -r '/^bin/,/^nobody/d' /etc/passwd  # 哪行至哪行用逗号 

19、从第一行开始,每隔两行显示一行

[root@localhost ~]# awk '{if(NR%3==1){print $0}}' /etc/passwd  # 如果是隔3行就%3,以此类推. 

20、每隔5行打印一个空行

[root@localhost ~]# awk '{if(NR%5==0){print $0;print "-----------"}else{print $0}}' /etc/passwd 

21、不显示指定字符的行

[root@localhost ~]# grep -vE 'g' 2.txt 

22、将文件中1到5行中aaa替换成AAA

[root@localhost ~]# sed -r '1,5s/aaa/AAA/g' 13.txt  # 替换用sed,行数在前面加就行了 

23、显示用户id为奇数的行

[root@localhost ~]# awk -F: '$3%2==1{print $0}' /etc/passwd 

24、显示系统普通用户,并打印系统用户名和id

[root@localhost ~]# awk -F: '$3>=1000{print $1, $3}' /etc/passwd 

25、统计nginx日志中独立用户数(ip维度计算)

[root@localhost ~]# awk '/([0-9]{1,3}.){3}[0-9]{1,3}/{dic[$1]++}END{for(i in dic){print i}}' access.log  

以后遇到计算一个参数出现的次数,就想到用字典的方式来计算

26、统计php.ini中每个词的个数

[root@localhost ~]# grep -oE '[0-9a-zA-Z]+' /etc/php.ini | awk '{dic[$1]++}END{for(i in dic){printf "%-15s | %-5dn", i, dic[i]}}' 

以后遇到计算一个参数出现的次数,就想到用字典的方式来计算