经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Linux/Shell » 查看文章
linux三剑客试题汇总
来源:cnblogs  作者:zong涵  时间:2021/12/24 8:56:45  对本文有异议

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

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

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

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

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

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

4、输出指定目录的基名

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

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

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

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

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

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

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

  1. [root@localhost /etc/sysconfig]# ip a | grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}'
  2. [root@localhost /etc/sysconfig]# ip a | sed -r -n '/([0-9]{1,3}\.){3}[0-9]{1,3}/p'
  3. [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函数出现的次数

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

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

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

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

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

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

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

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

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

13、查询出所有的qq邮箱

  1. [root@localhost ~]# grep -E '[0-9a-zA-Z-_]+@qq\.com'

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

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

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

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

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

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

17、显示奇数行

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

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

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

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

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

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

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

21、不显示指定字符的行

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

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

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

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

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

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

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

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

  1. [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中每个词的个数

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

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

原文链接:http://www.cnblogs.com/zonghan/p/15725169.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号