0

I have a file containing several switches and the boot variables.

lax1-sw0101#show boot
---------------------------
Switch 1
---------------------------
Current Boot Variables:
BOOT variable does not exist

Boot Variables on next reload:
BOOT variable does not exist
Manual Boot = no
Enable Break = no
Boot Mode = DEVICE
iPXE Timeout = 0
lax1-sw0101#

lgb1-sw0102#show boot
---------------------------
Switch 1
---------------------------
Current Boot Variables:
BOOT variable does not exist

Boot Variables on next reload:
BOOT variable does not exist
Manual Boot = no
Enable Break = no
Boot Mode = DEVICE
iPXE Timeout = 0
lgb1-sw0102#

las-sw0101#show boot
---------------------------
Switch 1
---------------------------
Current Boot Variables:
BOOT variable does not exist

Boot Variables on next reload:
BOOT variable = flash:/cat9k_iosxe.bin;
Manual Boot = no
Enable Break = no
Boot Mode = DEVICE
iPXE Timeout = 0
las-sw0101#

I need to filter only the switches which have the "Boot Variables on next reload:" as "Boot variables does not exist", and also print the hostname

Output:
lax1-sw0101#
Boot Variables on next reload:  
BOOT variable does not exist   

lgb1-sw0102#
Boot Variables on next reload:
BOOT variable does not exist

i have tried few awk/sed solutions like print between lines, print from nth line, the closest i can get to is with the one similar to below, but i cant get to print one line below

awk '/#/{a=$0}/Boot Variables on next reload/{print a"\n"$0}'
  • egrep "#|^Boot|^BOOT|^$" file this should get you started – Rui F Ribeiro Jan 17 at 17:55
0

Since you know what you're looking for, I'd suggest just printing the corresponding names with an awk state machine:

awk '/^Boot Variables on next reload:$/ { p=1 } 
     /^BOOT variable does not exist$/ && 1==p { p=2 } 
     /#$/ { if (2 == p) print; p=0; }' 
  input

This just uses a flag value, p, to indicate whether we've first seen Boot Variables on next reload:, then seen BOOT variable does not exist; when we see a line with a # at the end of the line, then we check the current flag value; only if the value is 2, print that (hostname) line. Regardless, we then reset the state back to the beginning.

If you need to print the static text, that's an easy addition to the print statement.

  • This AWK solution worked perfect.. also came up with this solution based on grep: grep -e "Manual Boot" -e "-sw" -B 1 file | grep -B 2 "BOOT variable does not exist" | grep -e "-sw" – Maran Ganesh Jan 28 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.