LinuxQuestions.org
Help answer threads with 0 replies.
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 05-22-2012, 12:28 PM   #1
kadalurpenguin
LQ Newbie
 
Registered: May 2012
Location: India
Distribution: RedHat, Cent, Fedora, Ubuntu
Posts: 3

Rep: Reputation: Disabled
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 12:30 PM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 05-22-2012, 12:45 PM   #2
em31amit
Member
 
Registered: Apr 2012
Location: /root
Distribution: Ubuntu, Redhat, Fedora, CentOS
Posts: 190

Rep: Reputation: 55
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.
Old 05-22-2012, 12:54 PM   #3
kadalurpenguin
LQ Newbie
 
Registered: May 2012
Location: India
Distribution: RedHat, Cent, Fedora, Ubuntu
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks a ton em31amit
Quote:
grep 'IP ' filename

that's it. and working on my machine.
Super it is working great.
 
Old 05-22-2012, 01:55 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Or simply use the -w option:
Code:
grep -w 192.168.0.1 ip-mac.txt
 
1 members found this post helpful.
Old 05-22-2012, 01:58 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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.
Old 05-22-2012, 09:05 PM   #6
kadalurpenguin
LQ Newbie
 
Registered: May 2012
Location: India
Distribution: RedHat, Cent, Fedora, Ubuntu
Posts: 3

Original Poster
Rep: Reputation: Disabled
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 09:06 PM.
 
  


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
grep multiple lines that the pattern matches in bash niharikaananth Linux - Newbie 7 03-13-2012 07:39 PM
help with grep... only print specific string with multiple matches? trey85stang Linux - General 2 05-11-2009 09:29 AM
Capturing only matches in grep/egrep via shell/bash koobi Programming 4 03-28-2009 12:37 PM
limiting line matches in grep genderbender Programming 5 07-05-2008 05:18 PM
grep regex . matches new lines?! lambchops468 Linux - Newbie 3 03-24-2008 09:19 PM

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

All times are GMT -5. The time now is 12:19 PM.

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