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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












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
    Jan 16 at 20:46
















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
    Jan 16 at 20:46














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 Jan 16 at 2:42









Maran GaneshMaran Ganesh

244




244












  • 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 at 20:46


















  • 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 at 20:46

















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 at 20:46






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 at 20:46











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
    Jan 16 at 21:24



















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
    Jan 16 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 =






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
      Jan 16 at 21:24
















    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
      Jan 16 at 21:24














    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 Jan 16 at 16:51









    Jeff Schaller

    40.8k1056129




    40.8k1056129










    answered Jan 16 at 16:33









    Praveen Kumar BSPraveen Kumar BS

    1,424138




    1,424138












    • ``` 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 at 21:24


















    • ``` 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 at 21:24

















    ``` 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 at 21:24






    ``` 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 at 21:24














    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
      Jan 16 at 16:43
















    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
      Jan 16 at 16:43














    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 Jan 16 at 2:54









    steeldriversteeldriver

    35.9k35286




    35.9k35286












    • 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 at 16:43


















    • 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 at 16:43

















    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 at 16:43






    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 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 =






    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 Jan 17 at 15:32









        Jeff Schaller

        40.8k1056129




        40.8k1056129










        answered Jan 16 at 3:23









        Emilio GalarragaEmilio Galarraga

        51929




        51929



























            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

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?