sort -r 将获得的信息进行降序排列
[root@tzc ~]# grep ‘Accept’ /var/log/secure | awk ‘{print $8}’ | uniq -c | sort -r
命令解释:grep 找到含Failed的信息行 后面是文件地址 awk是选择第几列 $(NF-3)表示倒数第三列
sort -r表示倒叙排序只有一位数的排序0-9 sort -nr表示第一列的倒序排序不限位数
[root@tzc ~]# grep ‘Failed’ /root/secure.log | awk ‘{print $(NF-3)}’ | uniq -c | sort -nr
数据量较大的处理 去重首先要排序 最后是查看前十个数据行
[root@tzc ~]# awk ‘{print $1}’ /root/nginx.log | sort |uniq -c |sort -rn |head -10
4156 47.92.239.154
1305 47.101.50.252
1096 120.244.106.104
827 125.106.191.210
767 192.227.86.114
653 64.71.78.13
620 192.99.225.97
581 185.104.106.72
580 139.99.195.212
516 223.72.73.169
案例1:获取当前登录用户的数量
分析:
查看用户登录信息的命令
who
统计命令
wc
word count
-l 统计行数
答案:
#who | wc -l
案例2:获取当前系统根分区使用情况
分析:
查看分区使用情况的命令
df
分隔字段
awk -F’’ ‘{print $n}’
答案:
#df / | tail -1 | awk ‘{print $5}’ | awk -F’%’ ‘{print $1}’
案例3:获取当前系统登录成功的IP地址top10
分析:
1.需要查看日志/var/log/secure
grep ‘Accept’ /var/log/secure
2.过滤出ip
grep ‘Accept’ secure.log | awk ‘{print $11}’
3.排序,去重
grep ‘Accept’ secure.log | awk ‘{print $11}’ | sort | uniq -c | sort -r | head
案例4:获取当前系统登录失败的IP地址top10
#grep ‘Fail’ secure.log | awk ‘{print $(NF-3)}’ | sort | uniq -c | sort -rn | head
输入重定向
[root@tzc ~]# wc -l /etc/passwd //统计文件里面的内容有多少行
23 /etc/passwd
[root@tzc ~]# wc -l< /etc/passwd //将文件里面的内容交给wc统计
23
输出重定向
[root@tzc ~]# ls
a anaconda-ks.cfg c d e nginx.log secure.log
[root@tzc ~]# ls /root/nginx.log
/root/nginx.log
[root@tzc ~]# ls /root/nginx.log >1.txt
[root@tzc ~]# cat 1.txt
/root/nginx.log
[root@tzc ~]# ls /root/nginx.log a
a /root/nginx.log
[root@tzc ~]# ls /root/nginx.log w
ls: 无法访问w: 没有那个文件或目录
/root/nginx.log
[root@tzc ~]# ls /root/w 2>2.txt
[root@tzc ~]# cat 2.txt
ls: 无法访问/root/w: 没有那个文件或目录
[root@tzc ~]# ls /root/nginx.log w &>3.txt
[root@tzc ~]# cat 3.txt
ls: 无法访问w: 没有那个文件或目录
/root/nginx.log
多行输出重定向
[root@tzc ~]# echo 'hellow
> hellow
> hellow' >4.txt
[root@tzc ~]# cat 4.txt
hellow
hellow
hellow
创建文件并写入
[root@tzc ~]# cat >6.txt <<END //输入END时候结束
> aaaa
> aaaa
> aaaa
> bbbb
> end
> END
[root@tzc ~]# cat 6.txt
aaaa
aaaa
aaaa
bbbb
end
>与>>之间的取别 >是输入覆盖 >>是追加输入
[root@tzc ~]# cat 4.txt
hellow
hellow
hellow
[root@tzc ~]# echo 1>4.txt
[root@tzc ~]# cat 4.txt
[root@tzc ~]# echo hellow>4.txt
[root@tzc ~]# cat 4.txt
hellow
[root@tzc ~]# cat 4.txt
helow world
[root@tzc ~]# echo who are you?>>4.txt
[root@tzc ~]# cat 4.txt
helow world
who are you?