0

I have a set of files containing boot variables from several cisco switches in the network. I have an requirement to filter only the switches with the boot variable empty on the next reload and print the hostname

given this data
hostname1#show boot
---------------------------
Switch 1
---------------------------
Current Boot Variables:
BOOT variable = flash:cat9k_iosxe.bin;

Boot Variables on next reload:
BOOT variable = 
Manual Boot = no
Enable Break = no
Boot Mode = DEVICE
iPXE Timeout = 0

hostname2#show boot
---------------------------
Switch 1
---------------------------
Current Boot Variables:
BOOT variable = flash:cat9k_iosxe.bin;

Boot Variables on next reload:
BOOT variable = flash:cat9k_iosxe.bin;
Manual Boot = no
Enable Break = no
Boot Mode = DEVICE
iPXE Timeout = 0
desired result
hostname1
BOOT variable =

Thanks!

1
  • Thanks for the answers .. still trying to get them work... Couple of catches..1. we cant play around with the keyword 'hostname' they are just examples.. they may be just random, meaning one can have jax-sw-1 and another can have lhr-sw-1 ... 2. There is a space after the equal sign in Boot variable = .. i am sorry if this changes anything in the answer.. will accept the answer(or the closest one) once i figure out.. if you have more inputs please share in here! Thanks – Maran Ganesh Jan 16 '19 at 20:46
1
awk '{a[++i]=$0}/BOOT variable =.$/{for(x=NR-10;x<=NR;x++)print a[x]}' filename|awk '/^hostname/||/BOOT variable =.$/{print $0}'| sed "s/#.*//g"

Results in:

hostname1
BOOT variable =
1
  • ``` awk '{a[++i]=$0}/BOOT variable = .$/{for(x=NR-10;x<=NR;x++)print a[x]}' results_* |awk '/#/||/BOOT variable = .$/{print $0}'| sed "s/#.*//g" ``` with some slight edits i was able to get this work.. thanks! – Maran Ganesh Jan 16 '19 at 21:24
4

You could do something like

awk -F'#' '
  $2 == "show boot" {hostname = $1} 
  /BOOT variable =[ \t]*$/ {print hostname; print}
' file
1
  • or awk -F '#| *= *' '$2 == "show boot" {host = $1} NF > 1 && $2 == "" {print host; print}' -- requires gawk I think for the complex FS. – glenn jackman Jan 16 '19 at 16:43
0

Using grep and pipes:

grep -B8 -E '= $' file |grep -E 'hostname|= $'

The first grep extracts the line where there's nothing after '=' symbol and 8 lines before the match to extract the line with hostname. The second grep filters the lines with hostname and BOOT variable =

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.