Choose a route from an export
Apply longest-prefix matching before metric and a deterministic final tie-break.
More specific comes first
Several routes can contain the same destination. In this simplified forwarding table, select the matching route with the longest prefix first. A default route with a smaller metric does not beat a more specific route merely because its metric is lower.
After prefix length, choose the smaller metric. If both are equal, use the lexically smallest route ID so repeated reports make the same choice. That last rule is a teaching tie-break, not a claim about every router's equal-cost forwarding behaviour. Real devices also consider route sources, administrative preferences and implementation-specific policy before constructing their forwarding tables.
Compare containment numerically
Convert the destination and each prefix to numeric bounds using the supplied helper. Keep route attributes together under their stable ID. A destination outside every prefix has no_route; never quietly assign it to whichever route happened to appear last in the file.
The exercise includes a default route, more-specific routes, a /32 host route and equal-cost ties. Reordering input rows must not change the result. The script analyses a supplied export only: it neither consults the browser guest's routing table nor modifies any live route. The input scope makes the selection algorithm explicit and testable.
awk -F '\t' 'NR>1 {printf "%s covers %s with metric %s\n",$1,$2,$3}' routes.tsv
Put it into practice.
routes.tsv contains validated unique route_id<TAB>prefix<TAB>metric<TAB>next_hop records; IDs start with a letter, metrics are nonnegative integers, and prefixes/next hops are IPv4. queries.tsv contains validated unique query_id<TAB>address rows. Print query_id<TAB>address<TAB>route_id<TAB>next_hop sorted by query ID. Select a containing route by longest prefix, then smallest metric, then lexical route ID under LC_ALL=C. If none match, print no_route and -. Use lib/ipv4.awk. This simplified table does not model routing-protocol preference or ECMP.
See an explained solution
export LC_ALL=C
printf 'query_id\taddress\troute_id\tnext_hop\n'
awk -F '\t' -f lib/ipv4.awk -f - routes.tsv queries.tsv <<'AWK' | sort
FILENAME==ARGV[1]{if(FNR>1 && cidr($2)){lo[$1]=first;hi[$1]=last;p[$1]=prefix;m[$1]=$3+0;hop[$1]=$4}next}
FNR>1{v=ipv4($2);best="";for(r in lo)if(v>=lo[r]&&v<=hi[r]){
if(best=="" || p[r]>p[best] || (p[r]==p[best] && (m[r]<m[best] || (m[r]==m[best] && ("x" r)<("x" best)))))best=r
}
printf "%s\t%s\t%s\t%s\n",$1,$2,(best==""?"no_route":best),(best==""?"-":hop[best])}
AWK
The selection compares an ordered set of criteria, so a low default-route metric cannot override a specific route. Explicit no-route output and lexical ties make the simplified forwarding decision reproducible across input and array order.