file-
xyz.161209:/userlogs/logs/reports 355G 195G 150G 57% /home
xyz.161209:/userlogs/logs/reports 355G 197G 148G 58% /home
xyz.161209:/userlogs/logs/reports 355G 201G 145G 59% /home
xyz.161210:/userlogs/logs/reports 355G 218G 129G 63% /home
xyz.161210:/userlogs/logs/reports 355G 223G 124G 65% /home
xyz.161210:/userlogs/logs/reports 355G 226G 121G 66% /home
xyz.161211:/userlogs/logs/reports 355G 210G 136G 61% /home
xyz.161211:/userlogs/logs/reports 355G 220G 127G 64% /home
xyz.161211:/userlogs/logs/reports 355G 173G 171G 51% /home
Result
xyz.161209:/userlogs/logs/reports 355G 201G 145G 59% /home
xyz.161210:/userlogs/logs/reports 355G 226G 121G 66% /home
xyz.161211:/userlogs/logs/reports 355G 220G 127G 64% /home
对于包含排序日期的每个第一列,按第一列分组并仅显示包含具有最大百分比的第五列的行。
这是我尝试过的,但是我无法获得所需的输出。有人可以帮助我吗?
awk -F, '{if ((a[substr($1,5,6)] == substr($1,5,6)) && (b[substr($5,1,2)] < substr($5,1,2)))b[substr($5,1,2)]=substr($5,1,2);}END{for(i in a){print i,a[i];}}' test.txt
答案1
这就是我想到的,怀疑这是最有效的方法,并希望看到更有效的方法。然而,它确实完成了工作
sort test.txt | awk -F':' '{print $1}' | uniq > unique.txt
while read p; do
grep $p test.txt | sort -r -k5 | head -1
done < unique.txt
rm unique.txt
解释:
sort test.txt | awk -F':' '{print $1}' | uniq > unique.txt
从列表中提取所有唯一的文件名
grep $p test.txt | sort -r -k5 | head -1
根据第 5 个字段(具有 % 值)降序排序并仅打印第一行。
答案2
这对我来说适用ksh
:
sort -nrk5 -t ' ' test.txt | sort -t '.' -unk2
给定这个测试文件:
otherfile_.161209:/userlogs/logs/reports 000G 000G 000G 55% /home somefile_.161209:/userlogs/logs/reports 000G 000G 000G 45% /home file71.161209:/userlogs/logs/reports 000G 000G 000G 71% /home file_longer_12.161209:/用户日志/日志/报告000G 000G 000G78%/家 qwerty_.161210:/userlogs/logs/reports 000G 000G 000G 31% /home xyz.161210:/userlogs/logs/reports 000G 000G 000G 34% /home abcdef.161210:/userlogs/logs/reports 000G 000G 000G85%/家 hellojoe_.161210:/userlogs/logs/reports 000G 000G 000G 45% /home kitchen_.161211:/userlogs/logs/reports 000G 000G 000G 39% /home room.161211:/userlogs/logs/reports 000G 000G 000G95%/家 Rooftop_77.161211:/userlogs/logs/reports 000G 000G 000G 12% /home f.161211:/userlogs/logs/reports 000G 000G 000G 30% /home
这是结果:
file_longer_12.161209:/用户日志/日志/报告000G 000G 000G78%/家 abcdef.161210:/userlogs/logs/reports 000G 000G 000G85%/家 room.161211:/userlogs/logs/reports 000G 000G 000G95%/家
因此它允许以下文件名:
- 有不同的长度
- 包含数字字符
分解:
sort -nrk5 -t ' '
:最初按第 5 列中的百分比排序sort -t '.' -unk2
:打印唯一的结果,仅从第一个字段计算日期字符串(使用.
分隔符)
答案3
这个 awk 怎么样
awk -F"[.: ]" '{if($(NF-1)+0>Arr[$2]+0){Arr[$2]=$(NF-1)+0;Res[$2]=$0}}END{for (i in Res){print Res[i]}}' file
xyz.161210:/userlogs/logs/reports 355G 226G 121G 66% /home
xyz.161211:/userlogs/logs/reports 355G 220G 127G 64% /home
xyz.161209:/userlogs/logs/reports 355G 201G 145G 59% /home
awk -F"[.: ]" '{
if($(NF-1)+0>Arr[$2]+0)
{
Arr[$2]=$(NF-1)+0;
Res[$2]=$0
}
}
END{
for (i in Res)
{
print Res[i]
}
}' file