top
Operator#
top — get first N sorted values of input sequence
Synopsis#
top [-r] [<const-expr> [<expr> [asc|desc] [nulls {first|last}] [, <expr> [asc|desc] [nulls {first|last}] ...]]]
Description#
The top
operator returns the first N values from a sequence sorted according
to the provided sort expressions. N is given by <const-expr>
, a compile-time
constant expression that evaluates to a positive integer. If <const-expr>
is
not provided, N defaults to 1
. Sort expressions and arguments behave as they
do for sort
. If no sort expression is provided, a sort key is
selected using same heuristic as sort
.
top
is functionally similar to sort
but is less resource
intensive because only the first N values are stored in memory (i.e., subsequent
values are discarded).
Examples#
Grab the smallest two values from a sequence of integers
top 2
1
5
3
9
23
7
Loading...
echo '1
5
3
9
23
7' \
| super -z -c 'top 2' -
Find the two names most frequently referenced in a sequence of records
count() by name | top -r 2 count
{name:"joe", age:22}
{name:"bob", age:37}
{name:"liz", age:25}
{name:"bob", age:18}
{name:"liz", age:34}
{name:"zoe", age:55}
{name:"ray", age:44}
{name:"sue", age:41}
{name:"liz", age:60}
Loading...
echo '{name:"joe", age:22}
{name:"bob", age:37}
{name:"liz", age:25}
{name:"bob", age:18}
{name:"liz", age:34}
{name:"zoe", age:55}
{name:"ray", age:44}
{name:"sue", age:41}
{name:"liz", age:60}' \
| super -z -c 'count() by name | top -r 2 count' -