LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-05-2013, 05:06 PM   #1
Anna1987
LQ Newbie
 
Registered: Feb 2013
Distribution: Fedora
Posts: 7

Rep: Reputation: Disabled
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,
 
Old 03-05-2013, 05:15 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
Old 03-06-2013, 12:18 AM   #3
Anna1987
LQ Newbie
 
Registered: Feb 2013
Distribution: Fedora
Posts: 7

Original Poster
Rep: Reputation: Disabled
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,
 
Old 03-06-2013, 01:00 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 03-06-2013, 01:04 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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
 
Old 03-06-2013, 01:17 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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?
 
Old 03-06-2013, 01:26 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by chrism01 View Post
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


I have to be honest: I did cheat (there's an extra space after the HWaddr......)
 
Old 03-06-2013, 01:28 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You sneaky devil

I'm definitely giving you Rep pts for that
 
Old 03-06-2013, 08:27 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
How about:
Code:
ip l | grep -oiP '(\w{2}:?){6}(?= )'
No pesky space
 
Old 03-09-2013, 07:56 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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}'

Last edited by David the H.; 03-09-2013 at 08:01 AM. Reason: addendum
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Condition in cp/ls | grep (regex, now I have two problems) Freddythunder Linux - Newbie 6 07-06-2012 08:39 AM
grep regex with wildcard yknot7 Linux - Newbie 6 11-10-2011 01:11 AM
[SOLVED] Combining regex in grep devUnix Linux - General 2 09-06-2011 11:11 AM
regex in ls vs. grep jhwilliams Linux - Software 2 08-10-2007 10:14 PM
grep (possibly regex) question. mwtheobald Linux - Newbie 1 08-17-2002 03:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration