shell题

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

[root@web02 ~]# egrep -i  '^s' /proc/meminfo 
[root@web02 ~]# awk  '/^[sS]/{print $0}' /proc/meminfo
[root@web02 ~]# sed -nr '/^[sS]/p' /proc/meminfo

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

[root@web02 ~]# egrep "^(root|centos|user)" /etc/passwd

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

[root@web02 ~]# grep -n -P ".*(w)()"    /etc/rc.d/init.d/functions
[root@web02 ~]# grep '()' /etc/rc.d/init.d/functions

4、输出指定目录的基名

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

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

[root@web02 ~]# ip a|egrep -o '([0-9]{1,3}.){3}[0-9]{1,3}'
[root@web02 ~]# ip a|egrep -o '[0-9]+'

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

[root@web02 ~]# awk -F: '{print $NF}' /etc/passwd|sort |uniq -c
#
#!/bin/bash

declare -A arrary

while read line
do
    res=`echo $line |awk -F: '{print $NF}'`
    let arrary[$res]++
done < 1.txt

for i in ${!arrary[*]}
do
    echo $i:${arrary[$i]}
done

[root@m01 ~]# awk -F: '{num[$NF]++}END{for (i in num){printf "%-15s : %s
",i,num[i]}}' /etc/passwd

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

ip a |egrep -o '([0-9]{1,3.){3}[0-9]{1,3}'

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

[root@web02 ~]# grep -ro "main" /etc/*.html|wc -L

find / -name "*.html" -o "*.php" -exec grep -R "main"  {} ;

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

[root@web02 ~]# egrep -v '^#|^$' php.ini
[root@web02 ~]# egrep "^[^#]" /etc/php.ini

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

[root@web02 ~]# egrep  ' +' 1.txt

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

[root@web02 ~]# egrep  '^# +' 3.txt 

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

[root@web02 ~]# egrep -Ro "root" /etc/|wc -w

13、查询出所有的qq邮箱

[root@web02 ~]# grep -Er "[0-9a-zA-Z-_.]+@qq.com" 1.txt

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

[root@web02 ~]# cat /var/log/nginx/error.log|awk '{print $3}'|wc -L
[root@web02 ~]# egrep -i 'error' /var/log/message

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

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

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

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

17、显示奇数行

[root@web02 ~]# awk -F: 'NR % 2 == 1{print NR,$0}' /etc/passwd
[root@web02 ~]# sed -rn '1~2p' /etc/passwd      #如果是偶数2~2p

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

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

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

awk -F: '{if(NR>3){num=(NR-3)%2; if(num){print $0}}}' /etc/passwd

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

awk -F: '{print $0;num=NR%5;if(!num){print ""}}' /etc/passwd

21、不显示指定字符的行

grep -v

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

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

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

[root@web02 ~]# awk -F: 'NR % 2 == 1{print NR,$0}' /etc/passwd
[root@web02 ~]# awk -F: '{if($3%2){print $0}}' /etc/passwd

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

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

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

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

26、实时打印nginx的访问ip

[root@web02 ~]# tail -f /var/log/nginx/access.log |awk '{print $1}'
[root@web02 ~]# tail -f /var/log/nginx/access.log | grep -oE '([0-9]{1,3}.){3}[0-9]{1,3}'

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

[root@web02 ~]# grep -Eow '[0-9a-zA-Z]+' /etc/php.ini | awk '{words[$1]++}END{for (i in words){print i,words[i]}}'

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

grep -oE '([0-9]{1,3}.){3}[0-9]{1,3}' /var/log/nginx/access.log | sort | uniq -c | sort -r | head 
原文地址:https://www.cnblogs.com/caodan01/p/14949225.html