Display a matching line and the closest preceding line matching another pattern

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











up vote
2
down vote

favorite












I have this requirement where show mac address table of a switch will list 1000 entries .




file1:

switch_1#show mac address table
AA:AA:AA:AA:AA:AA port 47
BB:BB:BB:BB:BB:BB port 48
.
.
.
DD:DD:DD:DD:DD:DD port 50



file2:

switch_2#show mac address table
AA:AA:AA:AA:AA:AA port 47
BB:BB:BB:BB:BB:BB port 48
.
.
.
DD:DD:DD:DD:DD:DD port 45


I need to find a way to display the line containing DD:DD:DD:DD:DD:DD and once this occurs, it should search few lines above and print the hostname containing line as well



So the output should look like:




switch_1#show mac address table
DD:DD:DD:DD:DD:DD port 50
switch_2#show mac address table
DD:DD:DD:DD:DD:DD port 45


file1, file2are stored in the same directory
(these are just examples, there is like 100+ files/switches and MAC needs to be filtered along with the switch names)










share|improve this question



























    up vote
    2
    down vote

    favorite












    I have this requirement where show mac address table of a switch will list 1000 entries .




    file1:

    switch_1#show mac address table
    AA:AA:AA:AA:AA:AA port 47
    BB:BB:BB:BB:BB:BB port 48
    .
    .
    .
    DD:DD:DD:DD:DD:DD port 50



    file2:

    switch_2#show mac address table
    AA:AA:AA:AA:AA:AA port 47
    BB:BB:BB:BB:BB:BB port 48
    .
    .
    .
    DD:DD:DD:DD:DD:DD port 45


    I need to find a way to display the line containing DD:DD:DD:DD:DD:DD and once this occurs, it should search few lines above and print the hostname containing line as well



    So the output should look like:




    switch_1#show mac address table
    DD:DD:DD:DD:DD:DD port 50
    switch_2#show mac address table
    DD:DD:DD:DD:DD:DD port 45


    file1, file2are stored in the same directory
    (these are just examples, there is like 100+ files/switches and MAC needs to be filtered along with the switch names)










    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have this requirement where show mac address table of a switch will list 1000 entries .




      file1:

      switch_1#show mac address table
      AA:AA:AA:AA:AA:AA port 47
      BB:BB:BB:BB:BB:BB port 48
      .
      .
      .
      DD:DD:DD:DD:DD:DD port 50



      file2:

      switch_2#show mac address table
      AA:AA:AA:AA:AA:AA port 47
      BB:BB:BB:BB:BB:BB port 48
      .
      .
      .
      DD:DD:DD:DD:DD:DD port 45


      I need to find a way to display the line containing DD:DD:DD:DD:DD:DD and once this occurs, it should search few lines above and print the hostname containing line as well



      So the output should look like:




      switch_1#show mac address table
      DD:DD:DD:DD:DD:DD port 50
      switch_2#show mac address table
      DD:DD:DD:DD:DD:DD port 45


      file1, file2are stored in the same directory
      (these are just examples, there is like 100+ files/switches and MAC needs to be filtered along with the switch names)










      share|improve this question















      I have this requirement where show mac address table of a switch will list 1000 entries .




      file1:

      switch_1#show mac address table
      AA:AA:AA:AA:AA:AA port 47
      BB:BB:BB:BB:BB:BB port 48
      .
      .
      .
      DD:DD:DD:DD:DD:DD port 50



      file2:

      switch_2#show mac address table
      AA:AA:AA:AA:AA:AA port 47
      BB:BB:BB:BB:BB:BB port 48
      .
      .
      .
      DD:DD:DD:DD:DD:DD port 45


      I need to find a way to display the line containing DD:DD:DD:DD:DD:DD and once this occurs, it should search few lines above and print the hostname containing line as well



      So the output should look like:




      switch_1#show mac address table
      DD:DD:DD:DD:DD:DD port 50
      switch_2#show mac address table
      DD:DD:DD:DD:DD:DD port 45


      file1, file2are stored in the same directory
      (these are just examples, there is like 100+ files/switches and MAC needs to be filtered along with the switch names)







      text-processing awk sed






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 8 at 22:11









      don_crissti

      47k15124154




      47k15124154










      asked Aug 8 at 21:54









      Maran Ganesh

      204




      204




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          awk solution



          If we get a line with a hash, store it in variable a.



          If we get a line with a matching MAC address, print variable a, along with the current line.



          $ awk '/#/a=$0/^DD:DD:DD:DD:DD:DD/print a"n"$0' file1 file2
          switch_1#show mac address table
          DD:DD:DD:DD:DD:DD port 50
          switch_2#show mac address table
          DD:DD:DD:DD:DD:DD port 45
          $


          sed solution



          If we get a line with a hash, place it in the hold space (h).



          If we get a line with a matching MAC address, append to the pattern space (H), copy hold space to pattern space (g), print pattern space (p)



          $ sed -n '/#/h;/^DD:DD:DD:DD:DD:DD/H;g;p' file1 file2
          switch_1#show mac address table
          DD:DD:DD:DD:DD:DD port 50
          switch_2#show mac address table
          DD:DD:DD:DD:DD:DD port 45
          $





          share|improve this answer


















          • 1




            or sed '/😀/h;/😩/!d;H;x if you're a golfer...
            – don_crissti
            Aug 8 at 22:36










          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          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%2f461377%2fdisplay-a-matching-line-and-the-closest-preceding-line-matching-another-pattern%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote



          accepted










          awk solution



          If we get a line with a hash, store it in variable a.



          If we get a line with a matching MAC address, print variable a, along with the current line.



          $ awk '/#/a=$0/^DD:DD:DD:DD:DD:DD/print a"n"$0' file1 file2
          switch_1#show mac address table
          DD:DD:DD:DD:DD:DD port 50
          switch_2#show mac address table
          DD:DD:DD:DD:DD:DD port 45
          $


          sed solution



          If we get a line with a hash, place it in the hold space (h).



          If we get a line with a matching MAC address, append to the pattern space (H), copy hold space to pattern space (g), print pattern space (p)



          $ sed -n '/#/h;/^DD:DD:DD:DD:DD:DD/H;g;p' file1 file2
          switch_1#show mac address table
          DD:DD:DD:DD:DD:DD port 50
          switch_2#show mac address table
          DD:DD:DD:DD:DD:DD port 45
          $





          share|improve this answer


















          • 1




            or sed '/😀/h;/😩/!d;H;x if you're a golfer...
            – don_crissti
            Aug 8 at 22:36














          up vote
          3
          down vote



          accepted










          awk solution



          If we get a line with a hash, store it in variable a.



          If we get a line with a matching MAC address, print variable a, along with the current line.



          $ awk '/#/a=$0/^DD:DD:DD:DD:DD:DD/print a"n"$0' file1 file2
          switch_1#show mac address table
          DD:DD:DD:DD:DD:DD port 50
          switch_2#show mac address table
          DD:DD:DD:DD:DD:DD port 45
          $


          sed solution



          If we get a line with a hash, place it in the hold space (h).



          If we get a line with a matching MAC address, append to the pattern space (H), copy hold space to pattern space (g), print pattern space (p)



          $ sed -n '/#/h;/^DD:DD:DD:DD:DD:DD/H;g;p' file1 file2
          switch_1#show mac address table
          DD:DD:DD:DD:DD:DD port 50
          switch_2#show mac address table
          DD:DD:DD:DD:DD:DD port 45
          $





          share|improve this answer


















          • 1




            or sed '/😀/h;/😩/!d;H;x if you're a golfer...
            – don_crissti
            Aug 8 at 22:36












          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          awk solution



          If we get a line with a hash, store it in variable a.



          If we get a line with a matching MAC address, print variable a, along with the current line.



          $ awk '/#/a=$0/^DD:DD:DD:DD:DD:DD/print a"n"$0' file1 file2
          switch_1#show mac address table
          DD:DD:DD:DD:DD:DD port 50
          switch_2#show mac address table
          DD:DD:DD:DD:DD:DD port 45
          $


          sed solution



          If we get a line with a hash, place it in the hold space (h).



          If we get a line with a matching MAC address, append to the pattern space (H), copy hold space to pattern space (g), print pattern space (p)



          $ sed -n '/#/h;/^DD:DD:DD:DD:DD:DD/H;g;p' file1 file2
          switch_1#show mac address table
          DD:DD:DD:DD:DD:DD port 50
          switch_2#show mac address table
          DD:DD:DD:DD:DD:DD port 45
          $





          share|improve this answer














          awk solution



          If we get a line with a hash, store it in variable a.



          If we get a line with a matching MAC address, print variable a, along with the current line.



          $ awk '/#/a=$0/^DD:DD:DD:DD:DD:DD/print a"n"$0' file1 file2
          switch_1#show mac address table
          DD:DD:DD:DD:DD:DD port 50
          switch_2#show mac address table
          DD:DD:DD:DD:DD:DD port 45
          $


          sed solution



          If we get a line with a hash, place it in the hold space (h).



          If we get a line with a matching MAC address, append to the pattern space (H), copy hold space to pattern space (g), print pattern space (p)



          $ sed -n '/#/h;/^DD:DD:DD:DD:DD:DD/H;g;p' file1 file2
          switch_1#show mac address table
          DD:DD:DD:DD:DD:DD port 50
          switch_2#show mac address table
          DD:DD:DD:DD:DD:DD port 45
          $






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 8 at 22:28

























          answered Aug 8 at 22:01









          steve

          12.9k22149




          12.9k22149







          • 1




            or sed '/😀/h;/😩/!d;H;x if you're a golfer...
            – don_crissti
            Aug 8 at 22:36












          • 1




            or sed '/😀/h;/😩/!d;H;x if you're a golfer...
            – don_crissti
            Aug 8 at 22:36







          1




          1




          or sed '/😀/h;/😩/!d;H;x if you're a golfer...
          – don_crissti
          Aug 8 at 22:36




          or sed '/😀/h;/😩/!d;H;x if you're a golfer...
          – don_crissti
          Aug 8 at 22:36

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f461377%2fdisplay-a-matching-line-and-the-closest-preceding-line-matching-another-pattern%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Peggy Mitchell

          The Forum (Inglewood, California)

          Palaiologos