{
for (i=1; i <= NF; i++)
{
print $i
}
printf "\n"
}
BEGIN {
FS = "\n"
RS = ""
}
{
for (i=1; i<=NF; i++)
printf "%s\t", $i
printf "\n"
}
{
oldline = $0
if ((getline) == 1) {
print $0
print oldline
} else {
print oldline
}
}
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.
sort +0 -1 +2 <filename>sorts <filename> first according to field 1 (unlike gawk, sort organizes fields from count zero), then within records that have the same value for field 1, it sorts according to the third field (the +2 part above). The -1 part instructs sort to ignore the second field. Typical sorting is ascending order of character. If you want a numeric sort, append n after the field numbers, e.g., sort +0 -1 +2n <filename>.