LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   REGEX with grep (http://www.linuxquestions.org/questions/linux-newbie-8/regex-with-grep-4175452850/)

Anna1987 03-05-2013 05:06 PM

REGEX with grep
 
Hello,

I have to create a grep question which shows MAC adress from ifconfig..

I did like this:
ifconfig -a | grep -E '[[:alnum:]]+:[[:alnum:]]+:+'

In my computer this command showed me MAC adresses.

But can anyone confirm me that it works correctly andin my commandthere is no errors
? or there are others solutions?

here is output:
[root@localhost grep]# ifconfig -a | grep -E '[[:alnum:]]+:[[:alnum:]]+:+'
ether 00:1b:38:1a:3a:2c txqueuelen 1000 (Ethernet)
inet6 fe80::21b:77ff:fe77:7b6e prefixlen 64 scopeid 0x20<link>
ether 00:1b:77:77:7b:6e txqueuelen 1000 (Ethernet)

thanks for your reply and any suggestion,

chrism01 03-05-2013 05:15 PM

If it works for, that's fine.
On my system (using your soln) I get
Code:

br0      Link encap:Ethernet  HWaddr E0:CB:4E:B9:5F:1A 
eth0      Link encap:Ethernet  HWaddr E0:CB:4E:B9:5F:1A 
eth1      Link encap:Ethernet  HWaddr 00:02:A5:42:D0:CC 
virbr0    Link encap:Ethernet  HWaddr 52:54:00:52:23:F5 
virbr0-nic Link encap:Ethernet  HWaddr 52:54:00:52:23:F5 

# I would do it this way
ifconfig|grep HW|awk '{print $5}'
E0:CB:4E:B9:5F:1A
E0:CB:4E:B9:5F:1A
52:54:00:52:23:F5


Anna1987 03-06-2013 12:18 AM

Yeah Ok thanks for your answer

But how can we do this without awk? only grep is possible and REGEX?

Du you have any idea?

Thanks for your reply,

chrism01 03-06-2013 01:00 AM

Code:

ifconfig | grep -E -o '([[:alnum:]]+:){5}[[:alnum:]]+'
E0:CB:4E:B9:5F:1A
E0:CB:4E:B9:5F:1A
52:54:00:52:23:F5

See http://linux.die.net/man/1/grep
NB: using '-a' on Linux includes installed but not active NICs.

druuna 03-06-2013 01:04 AM

Chris beaten me to it.

Here's one that has a regexp that's is a bit more restrictive:
Code:

ifconfig | grep -oiE '([0-9A-F]{2}:){5}[0-9A-F]{2}'
20:cf:30:39:bb:20
20:cf:30:39:bb:1f
42:3c:05:1a:38:e8
c6:d9:ab:a1:f8:38
86:26:78:65:33:73


chrism01 03-06-2013 01:17 AM

Hi drunna; here's a challenge. ;)
According to the man pages, ifconfig is deprecated and we should use the ip cmd http://linux.die.net/man/8/ip.
I'm struggling to find a soln to the OP's qn using only(!) ip & grep; keep getting the broadcast address in the results as well.
Can you do it? :)

druuna 03-06-2013 01:26 AM

Quote:

Originally Posted by chrism01 (Post 4905709)
Hi drunna; here's a challenge. ;)
According to the man pages, ifconfig is deprecated and we should use the ip cmd http://linux.die.net/man/8/ip.
I'm struggling to find a soln to the OP's qn using only(!) ip & grep; keep getting the broadcast address in the results as well.
Can you do it? :)

How about this;
Code:

ip link list | grep -oiE '([0-9A-F]{2}:){5}[0-9A-F]{2} '
00:00:00:00:00:00
20:cf:30:39:bb:20
20:cf:30:39:bb:1f
42:3c:05:1a:38:e8
86:26:78:65:33:73
c6:d9:ab:a1:f8:38

:D

I have to be honest: I did cheat (there's an extra space after the HWaddr......)

chrism01 03-06-2013 01:28 AM

You sneaky devil :) :)

I'm definitely giving you Rep pts for that :)

grail 03-06-2013 08:27 AM

How about:
Code:

ip l | grep -oiP '(\w{2}:?){6}(?= )'
No pesky space :)

David the H. 03-09-2013 07:56 AM

There's a regex character class expressly defined for hexdecimal, BTW: "[:xdigit:]". It's equivalent to "[0-9a-fA-F]".

PS: chrism01's solution in post #2 can be simplified to use awk only.

Code:

ifconfig | awk '/HWaddr/ {print $5}'


All times are GMT -5. The time now is 02:03 PM.