Cisco IOS – How to Match a Search String and Show Lines Above

cisco-ios

For example, I have a MAC address which i know is the MAC address on one of the interfaces. However when I search to what interface the MAC address belongs, it does not show me to what interface it belongs as this is on a separate line.

When using grep to go through a router config I can use grep -5 [MAC ADDRESS] to show 5 lines above and 5 lines below the line on which the search string is found. How ever MAC addresses are not in the running config, so is this possible?

A sort of workaround I use now is use sh interaces | begin [MAC ADDRESS]

If I know the interface below I know what interface the MAC address belongs to.

So is there an equivalent of grep -5 [MAC ADDRESS]?

Best Answer

I got this awnser from another forum:

I figured it out, but you need an IOS version that has IOS.sh:

term shell
sh ru | grep -u H.H.H | tail 10

The -u flag will show all of the lines above the mac address you're searching and then the tail command will just show the last ten lines of that output. You can substitute "10" with however many lines you want.

Prerequisites for Cisco IOS.sh

• Cisco IOS Release 15.1(4)M, 15.1(2)S, and later releases.

• Cisco IOS.sh must be configured and enabled to use the Cisco IOS.sh features and functions on yourrouter.

http://www.cisco.com/c/en/us/td/docs/ios/netmgmt/configuration/guide/Convert/IOS_Shell/nm_ios_shell.pdf