How to match for an empty string in a grep pattern search?












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!










share|improve this question























  • 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


















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!










share|improve this question























  • 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
















0












0








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!










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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





















  • 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












3 Answers
3






active

oldest

votes


















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 =





share|improve this answer


























  • ``` 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





















4














You could do something like



awk -F'#' '
$2 == "show boot" {hostname = $1}
/BOOT variable =[ t]*$/ {print hostname; print}
' file





share|improve this answer
























  • 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





















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 =






share|improve this answer

























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    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 =





    share|improve this answer


























    • ``` 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


















    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 =





    share|improve this answer


























    • ``` 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
















    1












    1








    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 =





    share|improve this answer















    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 =






    share|improve this answer














    share|improve this answer



    share|improve this answer








    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





















    • ``` 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















    4














    You could do something like



    awk -F'#' '
    $2 == "show boot" {hostname = $1}
    /BOOT variable =[ t]*$/ {print hostname; print}
    ' file





    share|improve this answer
























    • 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


















    4














    You could do something like



    awk -F'#' '
    $2 == "show boot" {hostname = $1}
    /BOOT variable =[ t]*$/ {print hostname; print}
    ' file





    share|improve this answer
























    • 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
















    4












    4








    4







    You could do something like



    awk -F'#' '
    $2 == "show boot" {hostname = $1}
    /BOOT variable =[ t]*$/ {print hostname; print}
    ' file





    share|improve this answer













    You could do something like



    awk -F'#' '
    $2 == "show boot" {hostname = $1}
    /BOOT variable =[ t]*$/ {print hostname; print}
    ' file






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 2 days ago









    steeldriversteeldriver

    35.5k35286




    35.5k35286













    • 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



















    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













    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 =






    share|improve this answer






























      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 =






      share|improve this answer




























        0












        0








        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 =






        share|improve this answer















        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 =







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 15 hours ago









        Jeff Schaller

        39.5k1054126




        39.5k1054126










        answered 2 days ago









        Emilio GalarragaEmilio Galarraga

        50929




        50929






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Loup dans la culture

            How to solve the problem of ntp “Unable to contact time server” from KDE?

            ASUS Zenbook UX433/UX333 — Configure Touchpad-embedded numpad on Linux