Linux基本命令-排序输入输出

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?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值