Thursday, August 18, 2016

Sed one-liners

I may occasionally publish small notes on clever commands I learn about.  Putting it here helps me store knowledge that my shoddy personal data management practices might otherwise lose...  One such note is a one-line sed command to print out the Linux interface(s) which handles the default route:
sed -n 's/\(^[^\t]*\)\t00000000.*/\1/p' /proc/net/route
An explanation, from left to right: Don't print each line (-n), prepare for a substitution ('s/), look for a string of non-tab characters at the beginning of the line (^[^\t]*) while saving the results (the \( and \) parts surrounding that), followed by a single tab and a string of eight 0s (\t00000000), followed by anything to gobble up the rest of the line (.*), then substitute it all with the non-tabs string saved earlier (/\1/), and print it (p').  The eight 0s represent the default route of

To be really specific the eight 0s specify a route for a network of undetermined size starting at  For the true default route, I should also check for a mask of 00000000, as OpenVPN sometimes adds two net routes ( and to avoid the need to replace the existing default route.  This command will find anything starting at as a default route, which may or may not be what you want...  In reality, since is reserved, if a route starts at zero, it's pretty defaulty anyway..