How to match for an empty string in a grep pattern search?
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!
awk sed grep file-search
add a comment |
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!
awk sed grep file-search
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
yesterday
add a comment |
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!
awk sed grep file-search
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!
awk sed grep file-search
awk sed grep file-search
asked 2 days ago
Maran GaneshMaran Ganesh
224
224
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
yesterday
add a comment |
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
yesterday
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
yesterday
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
yesterday
add a comment |
3 Answers
3
active
oldest
votes
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 =
``` 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
yesterday
add a comment |
You could do something like
awk -F'#' '
$2 == "show boot" {hostname = $1}
/BOOT variable =[ t]*$/ {print hostname; print}
' file
orawk -F '#| *= *' '$2 == "show boot" {host = $1} NF > 1 && $2 == "" {print host; print}'
-- requires gawk I think for the complex FS.
– glenn jackman
yesterday
add a comment |
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 =
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f494713%2fhow-to-match-for-an-empty-string-in-a-grep-pattern-search%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 =
``` 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
yesterday
add a comment |
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 =
``` 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
yesterday
add a comment |
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 =
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 =
edited yesterday
Jeff Schaller
39.5k1054126
39.5k1054126
answered yesterday
Praveen Kumar BSPraveen Kumar BS
1,340138
1,340138
``` 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
yesterday
add a comment |
``` 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
yesterday
``` 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
yesterday
``` 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
yesterday
add a comment |
You could do something like
awk -F'#' '
$2 == "show boot" {hostname = $1}
/BOOT variable =[ t]*$/ {print hostname; print}
' file
orawk -F '#| *= *' '$2 == "show boot" {host = $1} NF > 1 && $2 == "" {print host; print}'
-- requires gawk I think for the complex FS.
– glenn jackman
yesterday
add a comment |
You could do something like
awk -F'#' '
$2 == "show boot" {hostname = $1}
/BOOT variable =[ t]*$/ {print hostname; print}
' file
orawk -F '#| *= *' '$2 == "show boot" {host = $1} NF > 1 && $2 == "" {print host; print}'
-- requires gawk I think for the complex FS.
– glenn jackman
yesterday
add a comment |
You could do something like
awk -F'#' '
$2 == "show boot" {hostname = $1}
/BOOT variable =[ t]*$/ {print hostname; print}
' file
You could do something like
awk -F'#' '
$2 == "show boot" {hostname = $1}
/BOOT variable =[ t]*$/ {print hostname; print}
' file
answered 2 days ago
steeldriversteeldriver
35.5k35286
35.5k35286
orawk -F '#| *= *' '$2 == "show boot" {host = $1} NF > 1 && $2 == "" {print host; print}'
-- requires gawk I think for the complex FS.
– glenn jackman
yesterday
add a comment |
orawk -F '#| *= *' '$2 == "show boot" {host = $1} NF > 1 && $2 == "" {print host; print}'
-- requires gawk I think for the complex FS.
– glenn jackman
yesterday
or
awk -F '#| *= *' '$2 == "show boot" {host = $1} NF > 1 && $2 == "" {print host; print}'
-- requires gawk I think for the complex FS.– glenn jackman
yesterday
or
awk -F '#| *= *' '$2 == "show boot" {host = $1} NF > 1 && $2 == "" {print host; print}'
-- requires gawk I think for the complex FS.– glenn jackman
yesterday
add a comment |
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 =
add a comment |
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 =
add a comment |
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 =
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 =
edited 15 hours ago
Jeff Schaller
39.5k1054126
39.5k1054126
answered 2 days ago
Emilio GalarragaEmilio Galarraga
50929
50929
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f494713%2fhow-to-match-for-an-empty-string-in-a-grep-pattern-search%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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
yesterday