我有一个这样的文件:
1 12:00
1 12:34
1 01:01
1 08:06
2 09:56
2 06:52
...
我想从第一列的每个值中选择第二列的最大值。
新文件:
1 12:34
2 09:56
...
我怎样才能做到这一点??提前致谢!
awk '
{ if ($2>values[$1]) values[$1]=$2; }
END {
for (i in values) {
print i,values[i]
}
}
' file
这可能对您有用:
sort -k1,1n -k2,2r file | sort -uk1,1
perl -nale '$F[1]=~s/://;$h{$F[0]}=$F[1]if($F[1]>$h{$F[0]});
END{for(sort keys(%h)){($x=$h{$_})=~s/^(..)(..)$/\1:\2/;print"$_ $x"}}' file
仅重击;
# build arrays with 1.column value in its name
# use all digits in the row as index, the row as value
while read a b ; do
eval "array$a[$a${b//:/}]=\"$a $b\""
done < "$infile"
# select the last element of each array
for name in ${!array*}; do
last='${'${name}'[-1]}'
eval "echo ${last}"
done