(Video materials in preparation)
rank : Add a rank
Usage : rank [ref=<ref>] [key=<key>] <file>
Options : -e
-s<c>
Version : Tue Jan 9 09:02:34 JST 2024
Edition : 1
Add line numbers to "file". If a reference key field is specified
with "ref=<ref>", then the line numbers are restarted at 1 whenever
the value in the reference key field changes.
If a value field is specified with "key=<key>" then the same line
number is assigned when the value is the same.
<key> and <ref> designates the field position as fllows:
single field 2 the 2nd field
NF the last field
NF-1 the field just before the last field
contiguous fields 2/4 from the 2nd field to the 4th field
4/2 from the 4th field to the 2nd field
NF-3/NF from NF-3 field to the NF field
combination 2@NF the 2nd field and the NF field
There is no limit on the length of the key field or on the number
of key fields. The key field can also contain multi-byte characters
such as Japanese.
If you specify "r" as comparison method after the field position,
the fields are compared in reverse order. If you specify "n" as
comparison method after the field position, that field's values will
be compared as numbers. If you specify "nr" as comparison method
after the field, the values will be compared in reverse order as
numbers. If you specify comparison method before or after the "/",
you must use the same comparison method for both fields.
2n/5n OK
2n/5nr Error
2n/5r Error
When you specify "e" as comparison method or specify -e ootion and no
method, characters in the field are replaced as follows and compared
as string:
_ ==> 0x20 (space)
\0 ==> 0x00 (null)
\t ==> 0x09 (tab stop)
\n ==> 0x0a (new line)
\r ==> 0x0d (carrige return)
\_ ==> 0x5f (underscore)
\\ ==> 0x5c (back slash)
Simply assign line numbers without any arguments.
$ cat data
JPN Wilson_____ 26
JPN Hawking____ 27
JPN Newton_____ 27
JPN Tesla______ 30
USA BOB________ 25
USA GEORGE_____ 29
USA SAM________ 29
USA TOM________ 35
$ rank data
1 JPN Wilson_____ 26
2 JPN Hawking____ 27
3 JPN Newton_____ 27
4 JPN Tesla______ 30
5 USA BOB________ 25
6 USA GEORGE_____ 29
7 USA SAM________ 29
8 USA TOM________ 35
If you specify a reference field with "ref=<ref>" then when the
reference field value changes the line numbers are reset to 1.
$ rank ref=1 data
1 JPN Wilson_____ 26
2 JPN Hawking____ 27
3 JPN Newton_____ 27
4 JPN Tesla______ 30
1 USA BOB________ 25
2 USA GEORGE_____ 29
3 USA SAM________ 29
4 USA TOM________ 35
You can specify a value field as "key=<key>". In this case, records
where the key field has the same value are assigned the same line
number. Line numbers are skipped so that the following lines are
numbered properly.
$ rank key=3 data
1 JPN Wilson_____ 26
2 JPN Hawking____ 27
2 JPN Newton_____ 27
4 JPN Tesla______ 30
5 USA BOB________ 25
6 USA GEORGE_____ 29
6 USA SAM________ 29
8 USA TOM________ 35
When the reference key changes, line numbers are reset, and records
who share the same value field are assigned the same line number.
$ rank ref=1 key=3 data
1 JPN Wilson_____ 26
2 JPN Hawking____ 27
2 JPN Newton_____ 27
4 JPN Tesla______ 30
1 USA BOB________ 25
2 USA GEORGE_____ 29
2 USA SAM________ 29
4 USA TOM________ 35
* If the input file is omitted or specified as "-" then the command
will read from standard input.