Consider the input file silly containing the following
five lines:
2 is an even prime number.
3 is a crowd.
1 is next to a prime.
5 is first odd number after 4.
4 is just added here for completeness sake.
Let us suppose we want to sort the lines so that the line about
number 1 is first, then the line about number 2, and so on. Here's
a gawk script for this purpose:
{
store[$1] = $0
}
END {
for (i=1; i<=NR; i++)
print store[i]
}
Here we are using an associative array store to store
each line (in $0) alongside an index (which is the first field,
or $1). At the end of the day, we print all lines in sorted order
by going through the array store linearly. NR is
the number of records read thus far.