Extract MAC Address only from this text

Welcome Forums General PowerShell Q&A Extract MAC Address only from this text

Viewing 10 reply threads
  • Author
    Posts
    • #76683
      Participant
      Topics: 51
      Replies: 122
      Points: 78
      Rank: Member

      Hi,

      I want to extract the MAC address from this text:

      srv-ad.organization.local (192.168.46.30) em 00:0c:ff:ed:8e:78 [ether] em eth6

      I’ve tried to use .split(” “) and then Select-String ‘.*:.*:.*:.*:.*:.*’ but it leaves the MAC with two empty lines…
      Any better idea?

    • #76686
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,485
      Helping Hand
      Rank: Community MVP

      Regular expressions

      'srv-ad.organization.local (192.168.46.30) em 00:0c:ff:ed:8e:78 [ether] em eth6' -match '(([0-9a-f]{2}:){5}[0-9a-f]{2})'
      $Matches[0]
      • #76687
        Participant
        Topics: 51
        Replies: 122
        Points: 78
        Rank: Member

        Olaf Soyk,

        It didn’t worked…
        It shows the same text:

        PS C:\> $MAC
        srv-ad.organization.local (192.168.46.30) em 00:0c:ff:ec:8d:78 [ether] em eth6
        PS C:\> $MAC -match '(([0-9a-f]{2}:){5}[0-9a-f]{2})'
        srv-ad.organization.local (192.168.46.30) em 00:0c:ff:ec:8d:78 [ether] em eth6

        What worked for me was this:

        ($MAC.split(" ") | Select-String '.*:.*:.*:.*:.*:.*').Line
    • #76689
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,485
      Helping Hand
      Rank: Community MVP

      hmmm … that’s strange … the match operator should at least produce a true or false. Did you try the code just like I posted it?

    • #76690
      Participant
      Topics: 51
      Replies: 122
      Points: 78
      Rank: Member

      Olaf Soyk,

      Sorry, it wasn’t working, but I started a new console and worked perfeclty! Thanks!

    • #76693
      Participant
      Topics: 18
      Replies: 245
      Points: 108
      Helping Hand
      Rank: Participant

      This worked for me using Olaf’s suggestion

      $mac = ‘srv-ad.organization.local (192.168.46.30) em 00:0c:ff:ed:8e:78 [ether] em eth6’
      $result = $mac -match ‘(([0-9a-f]{2}:){5}[0-9a-f]{2})’
      $Matches[0]
      $result

      00:0c:ff:ed:8e:78
      True

    • #76695
      js
      Participant
      Topics: 29
      Replies: 813
      Points: 2,451
      Helping Hand
      Rank: Community Hero
      'srv-ad.organization.local (192.168.46.30) em 00:0c:ff:ed:8e:78 [ether] em eth6' | 
      foreach { -split $_ } | select -index 3
      
      00:0c:ff:ed:8e:78
      
      • #76696
        Participant
        Topics: 4
        Replies: 2247
        Points: 5,485
        Helping Hand
        Rank: Community MVP

        Neat! 🙂 But this works even shorter …

        ('srv-ad.organization.local (192.168.46.30) em 00:0c:ff:ed:8e:78 [ether] em eth6' -split ' ')[3]
      • #76704
        js
        Participant
        Topics: 29
        Replies: 813
        Points: 2,451
        Helping Hand
        Rank: Community Hero

        You can just do

        -split string

        . It splits on whitespace by default.

    • #76705
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,485
      Helping Hand
      Rank: Community MVP

      you mean like this, right?

      (-split 'srv-ad.organization.local (192.168.46.30) em 00:0c:ff:ed:8e:78 [ether] em eth6')[3]

      hmmm … that does not safe that much and is (at least for me) harder to read. But thanks anyway. 😉

      • #76716
        js
        Participant
        Topics: 29
        Replies: 813
        Points: 2,451
        Helping Hand
        Rank: Community Hero

        It is annoying that -split has to be in the front with no arguments, and in the back with a custom delimiter. Here’s a cut function I made, like the unix command. So you can just say “string | cut -f 3”:

        function cut {
          param(
            [Parameter(ValueFromPipeline=$True)] [string]$inputobject,
            [string]$delimiter='\s+',
            [string[]]$field
          )
        
            process {
            if ($field -eq $null) { $inputobject -split $delimiter } else {
              ($inputobject -split $delimiter)[$field] }
          }
        }
        
    • #76737
      Participant
      Topics: 6
      Replies: 667
      Points: 97
      Helping Hand
      Rank: Member

      Simpler RegEx

      'srv-ad.organization.local (192.168.46.30) em 00:0c:ff:ed:8e:78 [ether] em eth6' -match "\w\w:.*:\w\w"
      $matches.values

      Or the .Net way

      [regex]::Match('srv-ad.organization.local (192.168.46.30) em 00:0c:ff:ed:8e:78 [ether] em eth6', "\w\w:.*:\w\w").value
    • #76738
      Participant
      Topics: 5
      Replies: 322
      Points: 446
      Helping Hand
      Rank: Contributor

      Another way

      $string = 'srv-ad.organization.local (192.168.46.30) em 00:0c:ff:ed:8e:78 [ether] em eth6'
      $string -match '(\w{2}:)+\w{2}' ; $Matches[0]
      
    • #76741
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,485
      Helping Hand
      Rank: Community MVP

      Hmmm … call me picky but both of these approaches would even match “illegal” Mac addresses like “00:0c:ff:ed:8e:78:8e:78” or “00:0c:ff:ed:8y:78:xz:78” or “8y:78:xz:78”, right? 😉 😀

    • #76764
      Participant
      Topics: 6
      Replies: 667
      Points: 97
      Helping Hand
      Rank: Member

      You are right, it would. In context I saw no indication that it was user generated input, nor did I see request for content validation. I only saw request to pull the MAC from the string, which the regex accomplishes. If MAC format validation is required, then yes, it would be the more appropriate option.

Viewing 10 reply threads
  • The topic ‘Extract MAC Address only from this text’ is closed to new replies.