Find the answer to your Linux question:
Results 1 to 4 of 4
Hi every body, iam having a one text file like this ARPING 172.20.80.1 from 172.20.80.2 eth0 Unicast reply from 172.20.80.1 [ 00:14:1C:8F:F9:80 ] 6.070ms Sent 1 probes (1 broadcast(s)) Received ...
  1. #1
    Linux Newbie
    Join Date
    Oct 2006
    Posts
    107

    doubt on script

    Hi every body,

    iam having a one text file like this

    ARPING 172.20.80.1 from 172.20.80.2 eth0
    Unicast reply from 172.20.80.1 [00:14:1C:8F:F9:80] 6.070ms
    Sent 1 probes (1 broadcast(s))
    Received 1 response(s)

    so , i want to extract the "MAC ADDRESS" from this file and store it in one variable........

    what is the regular expression to extract the mac address from the file

    Pls can any body help me in this regard

    Thanks in Advance

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Code:
    #!/bin/sh
    macaddr=$(awk '/^Unicast/ { gsub(/\[|\]/,"",$5);print $5}' "file")
    echo $macaddr

  3. #3
    Linux Newbie
    Join Date
    Oct 2006
    Posts
    107

    thanks

    Thanks MR.

    i got the code and it is working , my problem is solved and thanks for your help.......

    But i didn't understand the code , can u explain it , if u don't mine .........

    and if possible , suggest me a material to learn these .......

    Once again thanks

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    english is not my first medium, but i will try to explain.

    Code:
    awk '/^Unicast/ { gsub(/\[|\]/,"",$5);print $5}' "file"
    awk is a pattern searching tool, i assume you already know that. Its syntax is
    awk 'pattern' {<action>}
    Using only your example input file and no other considerations, assumption is your MAC address occurs at the line which has the word "Unicast" as the start. To match that, we can use /^Unicast/. The ^ means match at start of the line. This is the 'pattern' in that awk syntax.
    Now comes the action. when we match "Unicast" what do we want to do. so we come to
    Code:
    gsub(/\[|\]/,"",$5)
    . $5 means the 5th field. By default awk uses the space field separator to parse your records. looking at "Unicast reply from 172.20.80.1 [00:14:1C:8F:F9:80] 6.070ms", the 5th field is your mac address with the square brackets. Correspondingly, the 1st field ($1) is "Unicast", 2nd field ($2) is "reply" and so on.
    awk has a function built in called gsub() that does string substitution. As the mac address we have are enclosed with [], gsub get rid of them. that is the gsub(/\[|\]/,"",$5) statement. Last, print out the value to be returned to the shell , as the macaddr variable.


    here's an online book which you can learn

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...