Extract a string that is located above and nearest to the matching pattern in a multiline output

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











up vote
0
down vote

favorite












Below is the HP ssacli command to see configured hardware RAID details:



ssacli ctrl slot=0 show config


and its output is as below:



HPE Smart Array P408i-a SR Gen10 in Slot 0 (Embedded)



Internal Drive Cage at Port 1I, Box 1, OK



Internal Drive Cage at Port 2I, Box 0, OK


Port Name: 1I (Mixed)

Port Name: 2I (Mixed)

Array A (Solid State SAS, Unused Space: 0 MB)

logicaldrive 1 (447.10 GB, RAID 1, OK)

physicaldrive 1I:1:1 (port 1I:box 1:bay 1, SAS SSD, 480 GB, OK)
physicaldrive 1I:1:2 (port 1I:box 1:bay 2, SAS SSD, 480 GB, OK)

SEP (Vendor ID HPE, Model Smart Adapter) 379 (Port: Unknown)


I have to figure out the Array name in order to delete it by searching for the matching disk info which I get as input from the user. For example, if the disk input is 1I:1:1 then I have to search for this string in the output of the above command. Since this disk is available and matching, I have to extract the Array name (here it is 'A') and once I get this Array parameter then I can go ahead delete this existing RAID configuration.



ssacli ctrl slot=0 show config | grep -B 4 '1I:1:1' | grep Array | awk 'print $2'


The problem with the above command is,



  • value 4 in the grep -B cannot be always constant as the matching disk may come first, second or third or so on under an Array in the output.


  • there may be multiple RAID array configurations available in the output, so there may be Array A, B, C etc., I have to find and retrieve the nearest "Array" string that matches my input disk







share|improve this question

























    up vote
    0
    down vote

    favorite












    Below is the HP ssacli command to see configured hardware RAID details:



    ssacli ctrl slot=0 show config


    and its output is as below:



    HPE Smart Array P408i-a SR Gen10 in Slot 0 (Embedded)



    Internal Drive Cage at Port 1I, Box 1, OK



    Internal Drive Cage at Port 2I, Box 0, OK


    Port Name: 1I (Mixed)

    Port Name: 2I (Mixed)

    Array A (Solid State SAS, Unused Space: 0 MB)

    logicaldrive 1 (447.10 GB, RAID 1, OK)

    physicaldrive 1I:1:1 (port 1I:box 1:bay 1, SAS SSD, 480 GB, OK)
    physicaldrive 1I:1:2 (port 1I:box 1:bay 2, SAS SSD, 480 GB, OK)

    SEP (Vendor ID HPE, Model Smart Adapter) 379 (Port: Unknown)


    I have to figure out the Array name in order to delete it by searching for the matching disk info which I get as input from the user. For example, if the disk input is 1I:1:1 then I have to search for this string in the output of the above command. Since this disk is available and matching, I have to extract the Array name (here it is 'A') and once I get this Array parameter then I can go ahead delete this existing RAID configuration.



    ssacli ctrl slot=0 show config | grep -B 4 '1I:1:1' | grep Array | awk 'print $2'


    The problem with the above command is,



    • value 4 in the grep -B cannot be always constant as the matching disk may come first, second or third or so on under an Array in the output.


    • there may be multiple RAID array configurations available in the output, so there may be Array A, B, C etc., I have to find and retrieve the nearest "Array" string that matches my input disk







    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Below is the HP ssacli command to see configured hardware RAID details:



      ssacli ctrl slot=0 show config


      and its output is as below:



      HPE Smart Array P408i-a SR Gen10 in Slot 0 (Embedded)



      Internal Drive Cage at Port 1I, Box 1, OK



      Internal Drive Cage at Port 2I, Box 0, OK


      Port Name: 1I (Mixed)

      Port Name: 2I (Mixed)

      Array A (Solid State SAS, Unused Space: 0 MB)

      logicaldrive 1 (447.10 GB, RAID 1, OK)

      physicaldrive 1I:1:1 (port 1I:box 1:bay 1, SAS SSD, 480 GB, OK)
      physicaldrive 1I:1:2 (port 1I:box 1:bay 2, SAS SSD, 480 GB, OK)

      SEP (Vendor ID HPE, Model Smart Adapter) 379 (Port: Unknown)


      I have to figure out the Array name in order to delete it by searching for the matching disk info which I get as input from the user. For example, if the disk input is 1I:1:1 then I have to search for this string in the output of the above command. Since this disk is available and matching, I have to extract the Array name (here it is 'A') and once I get this Array parameter then I can go ahead delete this existing RAID configuration.



      ssacli ctrl slot=0 show config | grep -B 4 '1I:1:1' | grep Array | awk 'print $2'


      The problem with the above command is,



      • value 4 in the grep -B cannot be always constant as the matching disk may come first, second or third or so on under an Array in the output.


      • there may be multiple RAID array configurations available in the output, so there may be Array A, B, C etc., I have to find and retrieve the nearest "Array" string that matches my input disk







      share|improve this question













      Below is the HP ssacli command to see configured hardware RAID details:



      ssacli ctrl slot=0 show config


      and its output is as below:



      HPE Smart Array P408i-a SR Gen10 in Slot 0 (Embedded)



      Internal Drive Cage at Port 1I, Box 1, OK



      Internal Drive Cage at Port 2I, Box 0, OK


      Port Name: 1I (Mixed)

      Port Name: 2I (Mixed)

      Array A (Solid State SAS, Unused Space: 0 MB)

      logicaldrive 1 (447.10 GB, RAID 1, OK)

      physicaldrive 1I:1:1 (port 1I:box 1:bay 1, SAS SSD, 480 GB, OK)
      physicaldrive 1I:1:2 (port 1I:box 1:bay 2, SAS SSD, 480 GB, OK)

      SEP (Vendor ID HPE, Model Smart Adapter) 379 (Port: Unknown)


      I have to figure out the Array name in order to delete it by searching for the matching disk info which I get as input from the user. For example, if the disk input is 1I:1:1 then I have to search for this string in the output of the above command. Since this disk is available and matching, I have to extract the Array name (here it is 'A') and once I get this Array parameter then I can go ahead delete this existing RAID configuration.



      ssacli ctrl slot=0 show config | grep -B 4 '1I:1:1' | grep Array | awk 'print $2'


      The problem with the above command is,



      • value 4 in the grep -B cannot be always constant as the matching disk may come first, second or third or so on under an Array in the output.


      • there may be multiple RAID array configurations available in the output, so there may be Array A, B, C etc., I have to find and retrieve the nearest "Array" string that matches my input disk









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 27 at 15:24









      SivaPrasath

      3,88611737




      3,88611737









      asked Jun 27 at 14:15









      Ibrahim Quraish

      1419




      1419




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          The following perl oneliner reads the file line by line, recording the last array until it meets the line with the physical drive '1I:1:1':



          ssacli ctrl slot=0 show config | perl -ne 'if ($_ =~ /Array (.)/) $array = $1; elsif ($_ =~ /physicaldrive 1I:1:1/) print $array; exit; '





          share|improve this answer





















          • Got from other forum as: awk -v disk='1I:1:1' '/^[[:space:]]*Array/ array=$2 $0 ~ disk print array; exit'
            – Ibrahim Quraish
            Jun 28 at 14:33










          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%2f452233%2fextract-a-string-that-is-located-above-and-nearest-to-the-matching-pattern-in-a%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
          1
          down vote



          accepted










          The following perl oneliner reads the file line by line, recording the last array until it meets the line with the physical drive '1I:1:1':



          ssacli ctrl slot=0 show config | perl -ne 'if ($_ =~ /Array (.)/) $array = $1; elsif ($_ =~ /physicaldrive 1I:1:1/) print $array; exit; '





          share|improve this answer





















          • Got from other forum as: awk -v disk='1I:1:1' '/^[[:space:]]*Array/ array=$2 $0 ~ disk print array; exit'
            – Ibrahim Quraish
            Jun 28 at 14:33














          up vote
          1
          down vote



          accepted










          The following perl oneliner reads the file line by line, recording the last array until it meets the line with the physical drive '1I:1:1':



          ssacli ctrl slot=0 show config | perl -ne 'if ($_ =~ /Array (.)/) $array = $1; elsif ($_ =~ /physicaldrive 1I:1:1/) print $array; exit; '





          share|improve this answer





















          • Got from other forum as: awk -v disk='1I:1:1' '/^[[:space:]]*Array/ array=$2 $0 ~ disk print array; exit'
            – Ibrahim Quraish
            Jun 28 at 14:33












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          The following perl oneliner reads the file line by line, recording the last array until it meets the line with the physical drive '1I:1:1':



          ssacli ctrl slot=0 show config | perl -ne 'if ($_ =~ /Array (.)/) $array = $1; elsif ($_ =~ /physicaldrive 1I:1:1/) print $array; exit; '





          share|improve this answer













          The following perl oneliner reads the file line by line, recording the last array until it meets the line with the physical drive '1I:1:1':



          ssacli ctrl slot=0 show config | perl -ne 'if ($_ =~ /Array (.)/) $array = $1; elsif ($_ =~ /physicaldrive 1I:1:1/) print $array; exit; '






          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Jun 27 at 14:51









          lgeorget

          8,50122448




          8,50122448











          • Got from other forum as: awk -v disk='1I:1:1' '/^[[:space:]]*Array/ array=$2 $0 ~ disk print array; exit'
            – Ibrahim Quraish
            Jun 28 at 14:33
















          • Got from other forum as: awk -v disk='1I:1:1' '/^[[:space:]]*Array/ array=$2 $0 ~ disk print array; exit'
            – Ibrahim Quraish
            Jun 28 at 14:33















          Got from other forum as: awk -v disk='1I:1:1' '/^[[:space:]]*Array/ array=$2 $0 ~ disk print array; exit'
          – Ibrahim Quraish
          Jun 28 at 14:33




          Got from other forum as: awk -v disk='1I:1:1' '/^[[:space:]]*Array/ array=$2 $0 ~ disk print array; exit'
          – Ibrahim Quraish
          Jun 28 at 14:33












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f452233%2fextract-a-string-that-is-located-above-and-nearest-to-the-matching-pattern-in-a%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

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

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay