#!/usr/bin/awk -f # # # ipsort [|-] # # Sort the input by the IP-number in the -th field # or remove the temporarily added ip-sort-field from the input. # BEGIN { STDERR = "/dev/stderr"; ipfield = ARGV[1]; ARGV[1] = ""; if (ipfield == "-") remove = 1; else if (ipfield+0 <= 0) { printf ("ipsort: no or zero ip-number field\n") >STDERR; exit(1); } } // { if (remove == 1) { sub(/^[^ ]+ /, "", $0); print $0; } else if (split($ipfield, x, ".") == 4) { str = sprintf("%03d.%03d.%03d.%03d", x[1], x[2], x[3], x[4]); print str, $0 | "sort"; } }