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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-22-2012, 01:28 PM
|
#1
|
LQ Newbie
Registered: May 2012
Location: India
Distribution: RedHat, Cent, Fedora, Ubuntu
Posts: 3
Rep:
|
grep showing all matches?
Hi All,
I have a file called ip-mac.txt in which IP and MAC addresses are stored.
Code:
$cat ip-mac.txt
192.168.0.1 00:00:00:00:00:01
192.168.0.2 00:00:00:00:00:02
192.168.0.3 00:00:00:00:00:03
192.168.0.4 00:00:00:00:00:04
192.168.0.5 00:00:00:00:00:05
192.168.0.6 00:00:00:00:00:06
192.168.0.7 00:00:00:00:00:07
192.168.0.8 00:00:00:00:00:08
192.168.0.9 00:00:00:00:00:09
192.168.0.10 00:00:00:00:00:10
192.168.0.11 00:00:00:00:00:11
192.168.0.12 00:00:00:00:00:12
192.168.0.13 00:00:00:00:00:13
192.168.0.14 00:00:00:00:00:14
192.168.0.15 00:00:00:00:00:15
192.168.0.16 00:00:00:00:00:16
192.168.0.20 00:00:00:00:00:20
When I search for "192.168.0.1", it is showing all of the matches even the command is without case insensitive.
Code:
$grep "192.168.0.1" ip-mac.txt
192.168.0.1 00:00:00:00:00:01
192.168.0.10 00:00:00:00:00:10
192.168.0.11 00:00:00:00:00:11
192.168.0.12 00:00:00:00:00:12
192.168.0.13 00:00:00:00:00:13
192.168.0.14 00:00:00:00:00:14
192.168.0.15 00:00:00:00:00:15
192.168.0.16 00:00:00:00:00:16
$grep "192.168.0.2" ip-mac.txt
192.168.0.2 00:00:00:00:00:02
192.168.0.20 00:00:00:00:00:20
Tried also without the quote and getting the same
Code:
$grep 192.168.0.2 ip-mac.txt
192.168.0.2 00:00:00:00:00:02
192.168.0.20 00:00:00:00:00:20
I don't know why and I need the result to be with only that IP for which I am doing grep. So please help me in how to get the result with only that particular IP and ignore all other matches.
Thanks in advance and your kind help would really be appreciated.
Last edited by kadalurpenguin; 05-22-2012 at 01:30 PM.
|
|
|
Click here to see the post LQ members have rated as the most helpful post in this thread.
|
05-22-2012, 01:45 PM
|
#2
|
Member
Registered: Apr 2012
Location: /root
Distribution: Ubuntu, Redhat, Fedora, CentOS
Posts: 190
Rep:
|
you can do something like this.
Code:
cat ip-mac.txt
192.168.0.1 00:00:00:00:00:01
192.168.0.2 00:00:00:00:00:02
192.168.0.3 00:00:00:00:00:03
192.168.0.4 00:00:00:00:00:04
192.168.0.5 00:00:00:00:00:05
192.168.0.6 00:00:00:00:00:06
192.168.0.7 00:00:00:00:00:07
192.168.0.8 00:00:00:00:00:08
192.168.0.9 00:00:00:00:00:09
192.168.0.10 00:00:00:00:00:10
192.168.0.11 00:00:00:00:00:11
192.168.0.12 00:00:00:00:00:12
192.168.0.13 00:00:00:00:00:13
192.168.0.14 00:00:00:00:00:14
192.168.0.15 00:00:00:00:00:15
192.168.0.16 00:00:00:00:00:16
192.168.0.20 00:00:00:00:00:20
$ grep '192.168.0.2 ' ip-mac.txt
192.168.0.2 00:00:00:00:00:02
you need to use single quote (') and use a blank space after last digit for eg. here i am denoting blank space with "B" so you need to use
grep '192.168.0.1B' filename
actual command
Code:
grep 'IP ' filename
that's it. and working on my machine.
|
|
2 members found this post helpful.
|
05-22-2012, 01:54 PM
|
#3
|
LQ Newbie
Registered: May 2012
Location: India
Distribution: RedHat, Cent, Fedora, Ubuntu
Posts: 3
Original Poster
Rep:
|
Thanks a ton em31amit
Quote:
grep 'IP ' filename
that's it. and working on my machine.
|
Super it is working great.
|
|
|
05-22-2012, 02:55 PM
|
#4
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Or simply use the -w option:
Code:
grep -w 192.168.0.1 ip-mac.txt
|
|
1 members found this post helpful.
|
05-22-2012, 02:58 PM
|
#5
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
The 'problem' you were facing, btw, is that you assume RegEx to
be word-centric. They're not. They're strictly character based,
and the solution above will fail if the separator is a TAB, for
example. You could use (e)grep, and either go with the -w option,
or you could use egrep with the a regex that makes use of word
boundaries, or grep with a regex that tackles both kinds of
in-line whitespace:
Code:
(e)grep -w 192.168.0.1
egrep "192.168.0.1\b"
grep "192.168.0.1[[:space:]]"
Cheers,
Tink
|
|
1 members found this post helpful.
|
05-22-2012, 10:05 PM
|
#6
|
LQ Newbie
Registered: May 2012
Location: India
Distribution: RedHat, Cent, Fedora, Ubuntu
Posts: 3
Original Poster
Rep:
|
Quote:
grep -w 192.168.0.1 ip-mac.txt
(e)grep -w 192.168.0.1
egrep "192.168.0.1\b"
grep "192.168.0.1[[:space:]]"
|
Super, all of the above codes are working fine.
Thank you all for giving more information about regex.
Last edited by kadalurpenguin; 05-22-2012 at 10:06 PM.
|
|
|
All times are GMT -5. The time now is 03:40 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|