awk

去重

[root@localhost tmp]# cat test.txt
hello world
awk
coding ants
hello world
awk
hello world
awk
coding ants
coding ants
[root@localhost tmp]# awk '!a[$0]++' test.txt
hello world
awk
coding ants
[root@localhost tmp]#

奇数

[root@localhost tmp]# cat -n /etc/passwd |awk 'i=!i'
 1	root:x:0:0:root:/root:/bin/bash
 3	daemon:x:2:2:daemon:/sbin:/sbin/nologin
 5	lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
 ...

偶数

[root@localhost tmp]# cat -n /etc/passwd |awk '!(i=!i)'
 2	bin:x:1:1:bin:/bin:/sbin/nologin
 4	adm:x:3:4:adm:/var/adm:/sbin/nologin
 6	sync:x:5:0:sync:/sbin:/bin/sync
...

过滤端口(+0 和不 +0的区别)

[root@localhost shell]# ss -ntpl4 |awk -F: '/sshd/{print $2}'
22                       *
[root@localhost shell]# ss -ntpl4 |awk -F: '/sshd/{print $2+0}'
22
[root@localhost shell]# 

找出关键字段,并排序

[root@localhost shell]# grep -R --include=*.xml '<need_robot_time>' . |sort -t_ -k3 -n

[root@localhost shell]# grep -R --include=*.xml '<need_robot_time>' . |xargs sed  -i 's#<need_robot_time>10#<need_robot_time>2'

seq 创建范围数值

1-25 1,2,3,4...25

[root@localhost shell]# seq -s ',' 25

统计1.txt 里面有多少个:

[root@localhost shell]# grep -o ':' 1.txt  |wc -l

在aaa后面追加ddd

:%s/aaa/&1:ddd/

打包排除日志

tar -zcvf dzpk.tar.gz --exclude=*.log .

查看/下占用

ls | egrep -v 'proc|data|dev' |xargs  -i du -sh {}

统计访问nginx的ip出现最多的前十个

[root@MyFirstwon nginx]# awk  '{++s[$1]}END{for(a in s)print s[a],a}' access.log |sort -nr | head 
66 66.249.79.206
31 66.249.79.208
31 49.95.168.91
31 113.57.28.120
30 219.143.183.2
29 66.249.79.210
28 66.249.79.202
25 171.104.111.98
25 123.125.34.11
24 66.249.79.204
[root@MyFirstwon nginx]# grep -i -o -E "([0-9]{1,3}\.){3}[0-9]{1,3}" access.log|sort -n |uniq -c |sort -nr|head    
     68 66.249.79.206
     31 66.249.79.208
     31 49.95.168.91
     31 113.57.28.120
     30 219.143.183.2
     29 66.249.79.210
     28 66.249.79.202
     25 171.104.111.98
     25 123.125.34.11
     24 66.249.79.204
[root@MyFirstwon nginx]#
comments powered by Disqus