image

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

[root@localhost ~]# grep -Ei "^s" /proc/meminfo

[root@localhost ~]# sed -nr '/^[sS]/p' /proc/meminfo

[root@localhost ~]# awk '/^[sS]/' /proc/meminfo

2、显示当前系统上的root,centos或者user的信息

[root@localhost ~]# grep -Erl "(root|centos|user)" /

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

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

4、输出指定目录的基名

[root@localhost ~]# echo /etc/passwd | grep -Eo "[^/]*$"

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

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

[root@localhost ~]# ip a |grep -Eo "[0-9]+"

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

[root@localhost ~]# awk -F: '{printf "用户名:%-20s 解释器:%-5s\n",$1,$NF}' /etc/passwd

7、过滤网卡中的ip,用三种方式实现

[root@localhost ~]# ip a | grep -Eo "([0-9]{1,3}\.+){3}[0-9]{1,3}"

[root@localhost ~]# ip a|grep "global" | awk '{print $2}' |awk -F / '{print $1}'

[root@localhost ~]# ip add | grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'

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

[root@localhost ~]# grep -Erl "(*.html|*.php)" /

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

[root@localhost ~]# grep -vE '^$|^#' /etc/php.ini

[root@localhost ~]# grep -E "^[^#]" /etc/php/.ini

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

[root@localhost ~]# grep -Er "(\ |\ +)" a.txt

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

[root@localhost ~]# grep -E "(^#|^#\ +)" a.txt

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

[root@localhost ~]# grep -Erw "root" /etc/ |wc -l

13、查询出所有的qq邮箱

[root@localhost ~]# grep -Er "[0-9]+\@([a-z]+){2}\.([a-z]+){3}" a.txt 

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

[root@localhost ~]# grep -nrw "error" /var/log/

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

[root@localhost ~]# egrep '^s' /etc/passwd | sed -r s/[a-zA-Z0-9]+$//g

[root@localhost ~]# sed -r 's/(^s.*\/)[a-z]+/\1/g' /etc/passwd

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

[root@localhost ~]# sed -ri '/[0-9]/d' a.txt

17、显示奇数行

[root@localhost ~]# awk 'NR%2' a.txt

[root@localhost ~]# awk '{if (NR%2){print $0}}' /etc/passwd

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

[root@localhost ~]# sed -r '/^bin /,/^nobody/d' /etc/passwd

19、从指定行开始,每隔两行显示一次

[root@localhost ~]# awk -F: '{if(NR>3){num=(NR-3)%2; if(num){print $0}}}' /etc/passwd

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

[root@localhost ~]# sed 'N;N;N;N;G' a.txt

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

21、不显示指定字符的行

[root@localhost ~]# grep -v a.txt

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

[root@localhost ~]# sed -r '1,5s/aaa/AAA/' a.txt

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

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

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

[root@localhost ~]# awk -F: '$3<500 && $3>0 {print $1,$3}' /etc/passwd

25、统计nginx日志中访问量(ip唯独计算)

[root@localhost ~]# grep -Ec '([0-9]{1,3}\.){3}[0-9]{1,3}' /var/log/nginx/access.log

26、实时打印nginx的访问ip

[root@localhost ~]# tail -f /var/log/nginx/access.log | awk '{print $1}'

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

[root@localhost ~]# egrep -o "[a-Z]+" pip.ini | wc -l | wc -c

28、统计1分钟内访问nginx次数超过10次的ip

百度的
#!/bin/bash
NGINX_LOG=/var/log/nginx/access.log
TIME=`date +%s`
DATE=`echo $TIME - 3600 | bc`
declare -A IP
while read line
do
	timestamp=`echo $line | grep -oE '[0-9]{4}.*T[0-9]{2}:[0-9]{2}:[0-9]{2}'`
	timestamp=`date -d "$timestamp" +%s`
	if (( $TIME >= $timestamp && $DATE <= $timestamp ));then
		ip=`echo $line| grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}'`
		number=`echo ${IP["$ip"]} | wc -L`
		[ $number -eq 0 ] && IP["$ip"]=0
		num=${IP["$ip"]}
		IP["$ip"]=`echo "$num + 1" | bc`
	fi
done < $NGINX_LOG
for i in ${!IP[*]}
do
	if (( ${IP[$i]} >= 10 ));then
		echo $i
	fi
done

29、找出nginx访问的峰值,按每个小时计算

!!! 百度的
#!/bin/bash

NGINX_LOG=/var/log/nginx/access.log

declare -A IP

while read line
do
	timestamp=`echo $line | grep -oE '[0-9]{4}.*T[0-9]{2}:[0-9]{2}:[0-9]{2}'`
	timestamp=`date -d "$timestamp" +%Y%m%d%H`

	number=`echo ${IP["$timestamp"]} | wc -L`
	[ $number -eq 0 ] && IP["$timestamp"]=0
	num=${IP["$timestamp"]}
	IP["$timestamp"]=`echo "$num + 1" | bc`
done < $NGINX_LOG

for i in ${!IP[*]}
do
	if (( ${IP[$i]} >= 10 ));then
		echo "$i ${IP[$i]}"
	fi
done

30、统计访问nginx前10的ip


image

posted on 2021-12-23 21:36  耿蜀黍  阅读(32)  评论(0编辑  收藏  举报